xref: /haiku/src/add-ons/kernel/busses/ata/promise_tx2/promise_tx2.c (revision 2222d0559df303a9846a2fad53741f8b20b14d7c)
1 /*
2  * Copyright 2002-04, Thomas Kurschel. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 /*
7 	Promise TX2 series IDE controller driver
8 */
9 
10 #include <KernelExport.h>
11 #include <stdlib.h>
12 #include <string.h>
13 
14 #include <ata_adapter.h>
15 
16 #define debug_level_flow 0
17 #define debug_level_error 3
18 #define debug_level_info 3
19 
20 #define DEBUG_MSG_PREFIX "PROMISE TX2 -- "
21 
22 #include "wrapper.h"
23 
24 #define PROMISE_TX2_CONTROLLER_MODULE_NAME "busses/ata/promise_tx2/device_v1"
25 #define PROMISE_TX2_CHANNEL_MODULE_NAME "busses/ata/promise_tx2/channel/v1"
26 
27 
28 static ata_for_controller_interface *ide;
29 static ata_adapter_interface *ata_adapter;
30 static device_manager_info *pnp;
31 
32 
33 static status_t
34 write_command_block_regs(void *channel_cookie, ata_task_file *tf, ata_reg_mask mask)
35 {
36 	return ata_adapter->write_command_block_regs((ata_adapter_channel_info *)channel_cookie, tf, mask);
37 }
38 
39 
40 static status_t
41 read_command_block_regs(void *channel_cookie, ata_task_file *tf, ata_reg_mask mask)
42 {
43 	return ata_adapter->read_command_block_regs((ata_adapter_channel_info *)channel_cookie, tf, mask);
44 }
45 
46 
47 static uint8
48 get_altstatus(void *channel_cookie)
49 {
50 	return ata_adapter->get_altstatus((ata_adapter_channel_info *)channel_cookie);
51 }
52 
53 
54 static status_t
55 write_device_control(void *channel_cookie, uint8 val)
56 {
57 	return ata_adapter->write_device_control((ata_adapter_channel_info *)channel_cookie, val);
58 }
59 
60 
61 static status_t
62 write_pio(void *channel_cookie, uint16 *data, int count, bool force_16bit)
63 {
64 	return ata_adapter->write_pio((ata_adapter_channel_info *)channel_cookie, data, count, force_16bit);
65 }
66 
67 
68 static status_t
69 read_pio(void *channel_cookie, uint16 *data, int count, bool force_16bit)
70 {
71 	return ata_adapter->read_pio((ata_adapter_channel_info *)channel_cookie, data, count, force_16bit);
72 }
73 
74 
75 static int32
76 inthand(void *arg)
77 {
78 	ata_adapter_channel_info *channel = (ata_adapter_channel_info *)arg;
79 	pci_device_module_info *pci = channel->pci;
80 	pci_device device = channel->device;
81 	ide_bm_status bm_status;
82 	uint8 status;
83 
84 	SHOW_FLOW0( 3, "" );
85 
86 	if (channel->lost)
87 		return B_UNHANDLED_INTERRUPT;
88 
89 	// the controller always tells us whether it generated the IRQ, so ask it first
90 	pci->write_io_8(device, channel->bus_master_base + 1, 0x0b);
91 	if ((pci->read_io_8(device, channel->bus_master_base + 3) & 0x20) == 0)
92 		return B_UNHANDLED_INTERRUPT;
93 
94 	return ata_adapter->inthand(arg);
95 }
96 
97 
98 static status_t
99 prepare_dma(void *channel_cookie, const physical_entry *sg_list,
100 	size_t sg_list_count, bool to_device)
101 {
102 	return ata_adapter->prepare_dma((ata_adapter_channel_info *)channel_cookie, sg_list, sg_list_count, to_device);
103 }
104 
105 
106 static status_t
107 start_dma(void *channel_cookie)
108 {
109 	return ata_adapter->start_dma((ata_adapter_channel_info *)channel_cookie);
110 }
111 
112 
113 static status_t
114 finish_dma(void *channel_cookie)
115 {
116 	return ata_adapter->finish_dma((ata_adapter_channel_info *)channel_cookie);
117 }
118 
119 
120 static status_t
121 init_channel(device_node_handle node, ata_channel ata_channel,
122 	void **channel_cookie)
123 {
124 	return ata_adapter->init_channel(node, ata_channel, (ata_adapter_channel_info **)channel_cookie,
125 		sizeof(ata_adapter_channel_info), inthand);
126 }
127 
128 
129 static status_t
130 uninit_channel(void *channel_cookie)
131 {
132 	return ata_adapter->uninit_channel((ata_adapter_channel_info *)channel_cookie);
133 }
134 
135 
136 static void channel_removed(device_node_handle node, void *channel_cookie)
137 {
138 	return ata_adapter->channel_removed(node, (ata_adapter_channel_info *)channel_cookie);
139 }
140 
141 
142 static status_t
143 init_controller(device_node_handle node, void *user_cookie,
144 	ata_adapter_controller_info **cookie)
145 {
146 	return ata_adapter->init_controller(node, user_cookie, cookie,
147 		sizeof(ata_adapter_controller_info));
148 }
149 
150 
151 static status_t
152 uninit_controller(ata_adapter_controller_info *controller)
153 {
154 	return ata_adapter->uninit_controller(controller);
155 }
156 
157 
158 static void
159 controller_removed(device_node_handle node, ata_adapter_controller_info *controller)
160 {
161 	return ata_adapter->controller_removed(node, controller);
162 }
163 
164 
165 // publish node of ide controller
166 
167 static status_t
168 publish_controller(device_node_handle parent, uint16 bus_master_base, uint8 intnum,
169 	io_resource_handle *resources, device_node_handle *node)
170 {
171 	device_attr attrs[] = {
172 		// info about ourself and our consumer
173 		{ B_DRIVER_MODULE, B_STRING_TYPE, { string: PROMISE_TX2_CONTROLLER_MODULE_NAME }},
174 
175 		// properties of this controller for ide bus manager
176 		// there are always max. 2 devices
177 		{ IDE_CONTROLLER_MAX_DEVICES_ITEM, B_UINT8_TYPE, { ui8: 2 }},
178 		// of course we can DMA
179 		{ IDE_CONTROLLER_CAN_DMA_ITEM, B_UINT8_TYPE, { ui8: 1 }},
180 		// command queuing always works
181 		{ IDE_CONTROLLER_CAN_CQ_ITEM, B_UINT8_TYPE, { ui8: 1 }},
182 		// choose any name here
183 		{ IDE_CONTROLLER_CONTROLLER_NAME_ITEM, B_STRING_TYPE, { string: "Promise TX2" }},
184 
185 		// DMA properties
186 		// some say it must be dword-aligned, others that it can be byte-aligned;
187 		// stay on the safe side
188 		{ B_DMA_ALIGNMENT, B_UINT32_TYPE, { ui32: 3 }},
189 		// one S/G block must not cross 64K boundary
190 		{ B_DMA_BOUNDARY, B_UINT32_TYPE, { ui32: 0xffff }},
191 		// size of S/G block is 16 bits with zero being 64K
192 		{ B_DMA_MAX_SEGMENT_BLOCKS, B_UINT32_TYPE, { ui32: 0x10000 }},
193 		{ B_DMA_MAX_SEGMENT_COUNT, B_UINT32_TYPE,
194 			{ ui32: ATA_ADAPTER_MAX_SG_COUNT }},
195 
196 		// private data to find controller
197 		{ ATA_ADAPTER_BUS_MASTER_BASE, B_UINT16_TYPE, { ui16: bus_master_base }},
198 		// store interrupt in controller node
199 		{ ATA_ADAPTER_INTNUM, B_UINT8_TYPE, { ui8: intnum }},
200 		{ NULL }
201 	};
202 
203 	SHOW_FLOW0(2, "");
204 
205 	return pnp->register_device(parent, attrs, resources, node);
206 }
207 
208 
209 // detect pure IDE controller, i.e. without channels
210 
211 static status_t
212 detect_controller(pci_device_module_info *pci, pci_device pci_device,
213 	device_node_handle parent, uint16 bus_master_base, int8 intnum,
214 	device_node_handle *node)
215 {
216 	io_resource_handle resource_handles[2];
217 
218 	SHOW_FLOW0(3, "");
219 
220 	if ((bus_master_base & PCI_address_space) != 1)
221 		return B_OK;
222 
223 	bus_master_base &= ~PCI_address_space;
224 
225 	{
226 		io_resource resources[2] = {
227 			{ IO_PORT, bus_master_base, 16 },
228 			{}
229 		};
230 
231 		if (pnp->acquire_io_resources(resources, resource_handles) != B_OK)
232 			return B_ERROR;
233 	}
234 
235 	return publish_controller(parent, bus_master_base, intnum, resource_handles, node);
236 }
237 
238 
239 static status_t
240 probe_controller(device_node_handle parent)
241 {
242 	pci_device_module_info *pci;
243 	pci_device device;
244 	uint16 command_block_base[2];
245 	uint16 control_block_base[2];
246 	uint16 bus_master_base;
247 	device_node_handle controller_node, channels[2];
248 	uint8 intnum;
249 	status_t res;
250 
251 	SHOW_FLOW0(3, "");
252 
253 	if (pnp->init_driver(parent, NULL, (driver_module_info **)&pci, (void **)&device) != B_OK)
254 		return B_ERROR;
255 
256 	command_block_base[0] = pci->read_pci_config(device, PCI_base_registers, 4);
257 	control_block_base[0] = pci->read_pci_config(device, PCI_base_registers + 4, 4);
258 	command_block_base[1] = pci->read_pci_config(device, PCI_base_registers + 8, 4);
259 	control_block_base[1] = pci->read_pci_config(device, PCI_base_registers + 12, 4);
260 	bus_master_base = pci->read_pci_config(device, PCI_base_registers + 16, 4);
261 	intnum = pci->read_pci_config(device, PCI_interrupt_line, 1);
262 
263 	command_block_base[0] &= PCI_address_io_mask;
264 	control_block_base[0] &= PCI_address_io_mask;
265 	command_block_base[1] &= PCI_address_io_mask;
266 	control_block_base[1] &= PCI_address_io_mask;
267 	bus_master_base &= PCI_address_io_mask;
268 
269 	res = detect_controller(pci, device, parent, bus_master_base, intnum, &controller_node);
270 	if (res != B_OK || controller_node == NULL)
271 		goto err;
272 
273 	ata_adapter->detect_channel(pci, device, controller_node,
274 		PROMISE_TX2_CHANNEL_MODULE_NAME, true,
275 		command_block_base[0], control_block_base[0], bus_master_base, intnum,
276 		0, "Primary Channel", &channels[0], false);
277 
278 	ata_adapter->detect_channel(pci, device, controller_node,
279 		PROMISE_TX2_CHANNEL_MODULE_NAME, true,
280 		command_block_base[1], control_block_base[1], bus_master_base, intnum,
281 		1, "Secondary Channel", &channels[1], false);
282 
283 	pnp->uninit_driver(parent);
284 
285 	return B_OK;
286 
287 err:
288 	pnp->uninit_driver(parent);
289 	return res;
290 }
291 
292 
293 static status_t
294 std_ops(int32 op, ...)
295 {
296 	switch (op) {
297 		case B_MODULE_INIT:
298 		case B_MODULE_UNINIT:
299 			return B_OK;
300 
301 		default:
302 			return B_ERROR;
303 	}
304 }
305 
306 
307 module_dependency module_dependencies[] = {
308 	{ IDE_FOR_CONTROLLER_MODULE_NAME, (module_info **)&ide },
309 	{ B_DEVICE_MANAGER_MODULE_NAME, (module_info **)&pnp },
310 	{ ATA_ADAPTER_MODULE_NAME, (module_info **)&ata_adapter },
311 	{}
312 };
313 
314 
315 // exported interface
316 static ide_controller_interface channel_interface = {
317 	{
318 		{
319 			PROMISE_TX2_CHANNEL_MODULE_NAME,
320 			0,
321 			std_ops
322 		},
323 
324 		NULL,	// supported devices
325 		NULL,
326 		(status_t (*)( device_node_handle , void *, void ** ))	init_channel,
327 		uninit_channel,
328 		channel_removed
329 	},
330 
331 	&write_command_block_regs,
332 	&read_command_block_regs,
333 
334 	&get_altstatus,
335 	&write_device_control,
336 
337 	&write_pio,
338 	&read_pio,
339 
340 	&prepare_dma,
341 	&start_dma,
342 	&finish_dma,
343 };
344 
345 
346 static driver_module_info controller_interface = {
347 	{
348 		PROMISE_TX2_CONTROLLER_MODULE_NAME,
349 		0,
350 		std_ops
351 	},
352 
353 	NULL,
354 	probe_controller,
355 	(status_t (*)(device_node_handle, void *, void **))	init_controller,
356 	(status_t (*)(void *))								uninit_controller,
357 	(void (*)(device_node_handle, void *))				controller_removed
358 };
359 
360 module_info *modules[] = {
361 	(module_info *)&controller_interface,
362 	(module_info *)&channel_interface,
363 	NULL
364 };
365