pata_it821x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /*
  2. * pata_it821x.c - IT821x PATA for new ATA layer
  3. * (C) 2005 Red Hat Inc
  4. * Alan Cox <alan@redhat.com>
  5. *
  6. * based upon
  7. *
  8. * it821x.c
  9. *
  10. * linux/drivers/ide/pci/it821x.c Version 0.09 December 2004
  11. *
  12. * Copyright (C) 2004 Red Hat <alan@redhat.com>
  13. *
  14. * May be copied or modified under the terms of the GNU General Public License
  15. * Based in part on the ITE vendor provided SCSI driver.
  16. *
  17. * Documentation available from
  18. * http://www.ite.com.tw/pc/IT8212F_V04.pdf
  19. * Some other documents are NDA.
  20. *
  21. * The ITE8212 isn't exactly a standard IDE controller. It has two
  22. * modes. In pass through mode then it is an IDE controller. In its smart
  23. * mode its actually quite a capable hardware raid controller disguised
  24. * as an IDE controller. Smart mode only understands DMA read/write and
  25. * identify, none of the fancier commands apply. The IT8211 is identical
  26. * in other respects but lacks the raid mode.
  27. *
  28. * Errata:
  29. * o Rev 0x10 also requires master/slave hold the same DMA timings and
  30. * cannot do ATAPI MWDMA.
  31. * o The identify data for raid volumes lacks CHS info (technically ok)
  32. * but also fails to set the LBA28 and other bits. We fix these in
  33. * the IDE probe quirk code.
  34. * o If you write LBA48 sized I/O's (ie > 256 sector) in smart mode
  35. * raid then the controller firmware dies
  36. * o Smart mode without RAID doesn't clear all the necessary identify
  37. * bits to reduce the command set to the one used
  38. *
  39. * This has a few impacts on the driver
  40. * - In pass through mode we do all the work you would expect
  41. * - In smart mode the clocking set up is done by the controller generally
  42. * but we must watch the other limits and filter.
  43. * - There are a few extra vendor commands that actually talk to the
  44. * controller but only work PIO with no IRQ.
  45. *
  46. * Vendor areas of the identify block in smart mode are used for the
  47. * timing and policy set up. Each HDD in raid mode also has a serial
  48. * block on the disk. The hardware extra commands are get/set chip status,
  49. * rebuild, get rebuild status.
  50. *
  51. * In Linux the driver supports pass through mode as if the device was
  52. * just another IDE controller. If the smart mode is running then
  53. * volumes are managed by the controller firmware and each IDE "disk"
  54. * is a raid volume. Even more cute - the controller can do automated
  55. * hotplug and rebuild.
  56. *
  57. * The pass through controller itself is a little demented. It has a
  58. * flaw that it has a single set of PIO/MWDMA timings per channel so
  59. * non UDMA devices restrict each others performance. It also has a
  60. * single clock source per channel so mixed UDMA100/133 performance
  61. * isn't perfect and we have to pick a clock. Thankfully none of this
  62. * matters in smart mode. ATAPI DMA is not currently supported.
  63. *
  64. * It seems the smart mode is a win for RAID1/RAID10 but otherwise not.
  65. *
  66. * TODO
  67. * - ATAPI and other speed filtering
  68. * - RAID configuration ioctls
  69. */
  70. #include <linux/kernel.h>
  71. #include <linux/module.h>
  72. #include <linux/pci.h>
  73. #include <linux/init.h>
  74. #include <linux/blkdev.h>
  75. #include <linux/delay.h>
  76. #include <scsi/scsi_host.h>
  77. #include <linux/libata.h>
  78. #define DRV_NAME "pata_it821x"
  79. #define DRV_VERSION "0.3.6"
  80. struct it821x_dev
  81. {
  82. unsigned int smart:1, /* Are we in smart raid mode */
  83. timing10:1; /* Rev 0x10 */
  84. u8 clock_mode; /* 0, ATA_50 or ATA_66 */
  85. u8 want[2][2]; /* Mode/Pri log for master slave */
  86. /* We need these for switching the clock when DMA goes on/off
  87. The high byte is the 66Mhz timing */
  88. u16 pio[2]; /* Cached PIO values */
  89. u16 mwdma[2]; /* Cached MWDMA values */
  90. u16 udma[2]; /* Cached UDMA values (per drive) */
  91. u16 last_device; /* Master or slave loaded ? */
  92. };
  93. #define ATA_66 0
  94. #define ATA_50 1
  95. #define ATA_ANY 2
  96. #define UDMA_OFF 0
  97. #define MWDMA_OFF 0
  98. /*
  99. * We allow users to force the card into non raid mode without
  100. * flashing the alternative BIOS. This is also neccessary right now
  101. * for embedded platforms that cannot run a PC BIOS but are using this
  102. * device.
  103. */
  104. static int it8212_noraid;
  105. /**
  106. * it821x_program - program the PIO/MWDMA registers
  107. * @ap: ATA port
  108. * @adev: Device to program
  109. * @timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
  110. *
  111. * Program the PIO/MWDMA timing for this channel according to the
  112. * current clock. These share the same register so are managed by
  113. * the DMA start/stop sequence as with the old driver.
  114. */
  115. static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
  116. {
  117. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  118. struct it821x_dev *itdev = ap->private_data;
  119. int channel = ap->port_no;
  120. u8 conf;
  121. /* Program PIO/MWDMA timing bits */
  122. if (itdev->clock_mode == ATA_66)
  123. conf = timing >> 8;
  124. else
  125. conf = timing & 0xFF;
  126. pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
  127. }
  128. /**
  129. * it821x_program_udma - program the UDMA registers
  130. * @ap: ATA port
  131. * @adev: ATA device to update
  132. * @timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
  133. *
  134. * Program the UDMA timing for this drive according to the
  135. * current clock. Handles the dual clocks and also knows about
  136. * the errata on the 0x10 revision. The UDMA errata is partly handled
  137. * here and partly in start_dma.
  138. */
  139. static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
  140. {
  141. struct it821x_dev *itdev = ap->private_data;
  142. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  143. int channel = ap->port_no;
  144. int unit = adev->devno;
  145. u8 conf;
  146. /* Program UDMA timing bits */
  147. if (itdev->clock_mode == ATA_66)
  148. conf = timing >> 8;
  149. else
  150. conf = timing & 0xFF;
  151. if (itdev->timing10 == 0)
  152. pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
  153. else {
  154. /* Early revision must be programmed for both together */
  155. pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
  156. pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
  157. }
  158. }
  159. /**
  160. * it821x_clock_strategy
  161. * @ap: ATA interface
  162. * @adev: ATA device being updated
  163. *
  164. * Select between the 50 and 66Mhz base clocks to get the best
  165. * results for this interface.
  166. */
  167. static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
  168. {
  169. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  170. struct it821x_dev *itdev = ap->private_data;
  171. u8 unit = adev->devno;
  172. struct ata_device *pair = ata_dev_pair(adev);
  173. int clock, altclock;
  174. u8 v;
  175. int sel = 0;
  176. /* Look for the most wanted clocking */
  177. if (itdev->want[0][0] > itdev->want[1][0]) {
  178. clock = itdev->want[0][1];
  179. altclock = itdev->want[1][1];
  180. } else {
  181. clock = itdev->want[1][1];
  182. altclock = itdev->want[0][1];
  183. }
  184. /* Master doesn't care does the slave ? */
  185. if (clock == ATA_ANY)
  186. clock = altclock;
  187. /* Nobody cares - keep the same clock */
  188. if (clock == ATA_ANY)
  189. return;
  190. /* No change */
  191. if (clock == itdev->clock_mode)
  192. return;
  193. /* Load this into the controller */
  194. if (clock == ATA_66)
  195. itdev->clock_mode = ATA_66;
  196. else {
  197. itdev->clock_mode = ATA_50;
  198. sel = 1;
  199. }
  200. pci_read_config_byte(pdev, 0x50, &v);
  201. v &= ~(1 << (1 + ap->port_no));
  202. v |= sel << (1 + ap->port_no);
  203. pci_write_config_byte(pdev, 0x50, v);
  204. /*
  205. * Reprogram the UDMA/PIO of the pair drive for the switch
  206. * MWDMA will be dealt with by the dma switcher
  207. */
  208. if (pair && itdev->udma[1-unit] != UDMA_OFF) {
  209. it821x_program_udma(ap, pair, itdev->udma[1-unit]);
  210. it821x_program(ap, pair, itdev->pio[1-unit]);
  211. }
  212. /*
  213. * Reprogram the UDMA/PIO of our drive for the switch.
  214. * MWDMA will be dealt with by the dma switcher
  215. */
  216. if (itdev->udma[unit] != UDMA_OFF) {
  217. it821x_program_udma(ap, adev, itdev->udma[unit]);
  218. it821x_program(ap, adev, itdev->pio[unit]);
  219. }
  220. }
  221. /**
  222. * it821x_passthru_set_piomode - set PIO mode data
  223. * @ap: ATA interface
  224. * @adev: ATA device
  225. *
  226. * Configure for PIO mode. This is complicated as the register is
  227. * shared by PIO and MWDMA and for both channels.
  228. */
  229. static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
  230. {
  231. /* Spec says 89 ref driver uses 88 */
  232. static const u16 pio[] = { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
  233. static const u8 pio_want[] = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
  234. struct it821x_dev *itdev = ap->private_data;
  235. int unit = adev->devno;
  236. int mode_wanted = adev->pio_mode - XFER_PIO_0;
  237. /* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
  238. itdev->want[unit][1] = pio_want[mode_wanted];
  239. itdev->want[unit][0] = 1; /* PIO is lowest priority */
  240. itdev->pio[unit] = pio[mode_wanted];
  241. it821x_clock_strategy(ap, adev);
  242. it821x_program(ap, adev, itdev->pio[unit]);
  243. }
  244. /**
  245. * it821x_passthru_set_dmamode - set initial DMA mode data
  246. * @ap: ATA interface
  247. * @adev: ATA device
  248. *
  249. * Set up the DMA modes. The actions taken depend heavily on the mode
  250. * to use. If UDMA is used as is hopefully the usual case then the
  251. * timing register is private and we need only consider the clock. If
  252. * we are using MWDMA then we have to manage the setting ourself as
  253. * we switch devices and mode.
  254. */
  255. static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  256. {
  257. static const u16 dma[] = { 0x8866, 0x3222, 0x3121 };
  258. static const u8 mwdma_want[] = { ATA_ANY, ATA_66, ATA_ANY };
  259. static const u16 udma[] = { 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
  260. static const u8 udma_want[] = { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
  261. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  262. struct it821x_dev *itdev = ap->private_data;
  263. int channel = ap->port_no;
  264. int unit = adev->devno;
  265. u8 conf;
  266. if (adev->dma_mode >= XFER_UDMA_0) {
  267. int mode_wanted = adev->dma_mode - XFER_UDMA_0;
  268. itdev->want[unit][1] = udma_want[mode_wanted];
  269. itdev->want[unit][0] = 3; /* UDMA is high priority */
  270. itdev->mwdma[unit] = MWDMA_OFF;
  271. itdev->udma[unit] = udma[mode_wanted];
  272. if (mode_wanted >= 5)
  273. itdev->udma[unit] |= 0x8080; /* UDMA 5/6 select on */
  274. /* UDMA on. Again revision 0x10 must do the pair */
  275. pci_read_config_byte(pdev, 0x50, &conf);
  276. if (itdev->timing10)
  277. conf &= channel ? 0x9F: 0xE7;
  278. else
  279. conf &= ~ (1 << (3 + 2 * channel + unit));
  280. pci_write_config_byte(pdev, 0x50, conf);
  281. it821x_clock_strategy(ap, adev);
  282. it821x_program_udma(ap, adev, itdev->udma[unit]);
  283. } else {
  284. int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
  285. itdev->want[unit][1] = mwdma_want[mode_wanted];
  286. itdev->want[unit][0] = 2; /* MWDMA is low priority */
  287. itdev->mwdma[unit] = dma[mode_wanted];
  288. itdev->udma[unit] = UDMA_OFF;
  289. /* UDMA bits off - Revision 0x10 do them in pairs */
  290. pci_read_config_byte(pdev, 0x50, &conf);
  291. if (itdev->timing10)
  292. conf |= channel ? 0x60: 0x18;
  293. else
  294. conf |= 1 << (3 + 2 * channel + unit);
  295. pci_write_config_byte(pdev, 0x50, conf);
  296. it821x_clock_strategy(ap, adev);
  297. }
  298. }
  299. /**
  300. * it821x_passthru_dma_start - DMA start callback
  301. * @qc: Command in progress
  302. *
  303. * Usually drivers set the DMA timing at the point the set_dmamode call
  304. * is made. IT821x however requires we load new timings on the
  305. * transitions in some cases.
  306. */
  307. static void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc)
  308. {
  309. struct ata_port *ap = qc->ap;
  310. struct ata_device *adev = qc->dev;
  311. struct it821x_dev *itdev = ap->private_data;
  312. int unit = adev->devno;
  313. if (itdev->mwdma[unit] != MWDMA_OFF)
  314. it821x_program(ap, adev, itdev->mwdma[unit]);
  315. else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
  316. it821x_program_udma(ap, adev, itdev->udma[unit]);
  317. ata_bmdma_start(qc);
  318. }
  319. /**
  320. * it821x_passthru_dma_stop - DMA stop callback
  321. * @qc: ATA command
  322. *
  323. * We loaded new timings in dma_start, as a result we need to restore
  324. * the PIO timings in dma_stop so that the next command issue gets the
  325. * right clock values.
  326. */
  327. static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
  328. {
  329. struct ata_port *ap = qc->ap;
  330. struct ata_device *adev = qc->dev;
  331. struct it821x_dev *itdev = ap->private_data;
  332. int unit = adev->devno;
  333. ata_bmdma_stop(qc);
  334. if (itdev->mwdma[unit] != MWDMA_OFF)
  335. it821x_program(ap, adev, itdev->pio[unit]);
  336. }
  337. /**
  338. * it821x_passthru_dev_select - Select master/slave
  339. * @ap: ATA port
  340. * @device: Device number (not pointer)
  341. *
  342. * Device selection hook. If neccessary perform clock switching
  343. */
  344. static void it821x_passthru_dev_select(struct ata_port *ap,
  345. unsigned int device)
  346. {
  347. struct it821x_dev *itdev = ap->private_data;
  348. if (itdev && device != itdev->last_device) {
  349. struct ata_device *adev = &ap->device[device];
  350. it821x_program(ap, adev, itdev->pio[adev->devno]);
  351. itdev->last_device = device;
  352. }
  353. ata_std_dev_select(ap, device);
  354. }
  355. /**
  356. * it821x_smart_qc_issue_prot - wrap qc issue prot
  357. * @qc: command
  358. *
  359. * Wrap the command issue sequence for the IT821x. We need to
  360. * perform out own device selection timing loads before the
  361. * usual happenings kick off
  362. */
  363. static unsigned int it821x_smart_qc_issue_prot(struct ata_queued_cmd *qc)
  364. {
  365. switch(qc->tf.command)
  366. {
  367. /* Commands the firmware supports */
  368. case ATA_CMD_READ:
  369. case ATA_CMD_READ_EXT:
  370. case ATA_CMD_WRITE:
  371. case ATA_CMD_WRITE_EXT:
  372. case ATA_CMD_PIO_READ:
  373. case ATA_CMD_PIO_READ_EXT:
  374. case ATA_CMD_PIO_WRITE:
  375. case ATA_CMD_PIO_WRITE_EXT:
  376. case ATA_CMD_READ_MULTI:
  377. case ATA_CMD_READ_MULTI_EXT:
  378. case ATA_CMD_WRITE_MULTI:
  379. case ATA_CMD_WRITE_MULTI_EXT:
  380. case ATA_CMD_ID_ATA:
  381. /* Arguably should just no-op this one */
  382. case ATA_CMD_SET_FEATURES:
  383. return ata_qc_issue_prot(qc);
  384. }
  385. printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", qc->tf.command);
  386. return AC_ERR_INVALID;
  387. }
  388. /**
  389. * it821x_passthru_qc_issue_prot - wrap qc issue prot
  390. * @qc: command
  391. *
  392. * Wrap the command issue sequence for the IT821x. We need to
  393. * perform out own device selection timing loads before the
  394. * usual happenings kick off
  395. */
  396. static unsigned int it821x_passthru_qc_issue_prot(struct ata_queued_cmd *qc)
  397. {
  398. it821x_passthru_dev_select(qc->ap, qc->dev->devno);
  399. return ata_qc_issue_prot(qc);
  400. }
  401. /**
  402. * it821x_smart_set_mode - mode setting
  403. * @ap: interface to set up
  404. * @unused: device that failed (error only)
  405. *
  406. * Use a non standard set_mode function. We don't want to be tuned.
  407. * The BIOS configured everything. Our job is not to fiddle. We
  408. * read the dma enabled bits from the PCI configuration of the device
  409. * and respect them.
  410. */
  411. static int it821x_smart_set_mode(struct ata_port *ap, struct ata_device **unused)
  412. {
  413. int dma_enabled = 0;
  414. int i;
  415. /* Bits 5 and 6 indicate if DMA is active on master/slave */
  416. /* It is possible that BMDMA isn't allocated */
  417. if (ap->ioaddr.bmdma_addr)
  418. dma_enabled = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  419. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  420. struct ata_device *dev = &ap->device[i];
  421. if (ata_dev_enabled(dev)) {
  422. /* We don't really care */
  423. dev->pio_mode = XFER_PIO_0;
  424. dev->dma_mode = XFER_MW_DMA_0;
  425. /* We do need the right mode information for DMA or PIO
  426. and this comes from the current configuration flags */
  427. if (dma_enabled & (1 << (5 + i))) {
  428. ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
  429. dev->xfer_mode = XFER_MW_DMA_0;
  430. dev->xfer_shift = ATA_SHIFT_MWDMA;
  431. dev->flags &= ~ATA_DFLAG_PIO;
  432. } else {
  433. ata_dev_printk(dev, KERN_INFO, "configured for PIO\n");
  434. dev->xfer_mode = XFER_PIO_0;
  435. dev->xfer_shift = ATA_SHIFT_PIO;
  436. dev->flags |= ATA_DFLAG_PIO;
  437. }
  438. }
  439. }
  440. return 0;
  441. }
  442. /**
  443. * it821x_dev_config - Called each device identify
  444. * @adev: Device that has just been identified
  445. *
  446. * Perform the initial setup needed for each device that is chip
  447. * special. In our case we need to lock the sector count to avoid
  448. * blowing the brains out of the firmware with large LBA48 requests
  449. *
  450. * FIXME: When FUA appears we need to block FUA too. And SMART and
  451. * basically we need to filter commands for this chip.
  452. */
  453. static void it821x_dev_config(struct ata_device *adev)
  454. {
  455. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  456. ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  457. if (adev->max_sectors > 255)
  458. adev->max_sectors = 255;
  459. if (strstr(model_num, "Integrated Technology Express")) {
  460. /* RAID mode */
  461. printk(KERN_INFO "IT821x %sRAID%d volume",
  462. adev->id[147]?"Bootable ":"",
  463. adev->id[129]);
  464. if (adev->id[129] != 1)
  465. printk("(%dK stripe)", adev->id[146]);
  466. printk(".\n");
  467. }
  468. }
  469. /**
  470. * it821x_check_atapi_dma - ATAPI DMA handler
  471. * @qc: Command we are about to issue
  472. *
  473. * Decide if this ATAPI command can be issued by DMA on this
  474. * controller. Return 0 if it can be.
  475. */
  476. static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
  477. {
  478. struct ata_port *ap = qc->ap;
  479. struct it821x_dev *itdev = ap->private_data;
  480. /* No ATAPI DMA in smart mode */
  481. if (itdev->smart)
  482. return -EOPNOTSUPP;
  483. /* No ATAPI DMA on rev 10 */
  484. if (itdev->timing10)
  485. return -EOPNOTSUPP;
  486. /* Cool */
  487. return 0;
  488. }
  489. /**
  490. * it821x_port_start - port setup
  491. * @ap: ATA port being set up
  492. *
  493. * The it821x needs to maintain private data structures and also to
  494. * use the standard PCI interface which lacks support for this
  495. * functionality. We instead set up the private data on the port
  496. * start hook, and tear it down on port stop
  497. */
  498. static int it821x_port_start(struct ata_port *ap)
  499. {
  500. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  501. struct it821x_dev *itdev;
  502. u8 conf;
  503. int ret = ata_port_start(ap);
  504. if (ret < 0)
  505. return ret;
  506. itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
  507. if (itdev == NULL)
  508. return -ENOMEM;
  509. ap->private_data = itdev;
  510. pci_read_config_byte(pdev, 0x50, &conf);
  511. if (conf & 1) {
  512. itdev->smart = 1;
  513. /* Long I/O's although allowed in LBA48 space cause the
  514. onboard firmware to enter the twighlight zone */
  515. /* No ATAPI DMA in this mode either */
  516. }
  517. /* Pull the current clocks from 0x50 */
  518. if (conf & (1 << (1 + ap->port_no)))
  519. itdev->clock_mode = ATA_50;
  520. else
  521. itdev->clock_mode = ATA_66;
  522. itdev->want[0][1] = ATA_ANY;
  523. itdev->want[1][1] = ATA_ANY;
  524. itdev->last_device = -1;
  525. pci_read_config_byte(pdev, PCI_REVISION_ID, &conf);
  526. if (conf == 0x10) {
  527. itdev->timing10 = 1;
  528. /* Need to disable ATAPI DMA for this case */
  529. if (!itdev->smart)
  530. printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n");
  531. }
  532. return 0;
  533. }
  534. static struct scsi_host_template it821x_sht = {
  535. .module = THIS_MODULE,
  536. .name = DRV_NAME,
  537. .ioctl = ata_scsi_ioctl,
  538. .queuecommand = ata_scsi_queuecmd,
  539. .can_queue = ATA_DEF_QUEUE,
  540. .this_id = ATA_SHT_THIS_ID,
  541. .sg_tablesize = LIBATA_MAX_PRD,
  542. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  543. .emulated = ATA_SHT_EMULATED,
  544. .use_clustering = ATA_SHT_USE_CLUSTERING,
  545. .proc_name = DRV_NAME,
  546. .dma_boundary = ATA_DMA_BOUNDARY,
  547. .slave_configure = ata_scsi_slave_config,
  548. .slave_destroy = ata_scsi_slave_destroy,
  549. .bios_param = ata_std_bios_param,
  550. };
  551. static struct ata_port_operations it821x_smart_port_ops = {
  552. .set_mode = it821x_smart_set_mode,
  553. .port_disable = ata_port_disable,
  554. .tf_load = ata_tf_load,
  555. .tf_read = ata_tf_read,
  556. .mode_filter = ata_pci_default_filter,
  557. .check_status = ata_check_status,
  558. .check_atapi_dma= it821x_check_atapi_dma,
  559. .exec_command = ata_exec_command,
  560. .dev_select = ata_std_dev_select,
  561. .dev_config = it821x_dev_config,
  562. .freeze = ata_bmdma_freeze,
  563. .thaw = ata_bmdma_thaw,
  564. .error_handler = ata_bmdma_error_handler,
  565. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  566. .cable_detect = ata_cable_unknown,
  567. .bmdma_setup = ata_bmdma_setup,
  568. .bmdma_start = ata_bmdma_start,
  569. .bmdma_stop = ata_bmdma_stop,
  570. .bmdma_status = ata_bmdma_status,
  571. .qc_prep = ata_qc_prep,
  572. .qc_issue = it821x_smart_qc_issue_prot,
  573. .data_xfer = ata_data_xfer,
  574. .irq_handler = ata_interrupt,
  575. .irq_clear = ata_bmdma_irq_clear,
  576. .irq_on = ata_irq_on,
  577. .irq_ack = ata_irq_ack,
  578. .port_start = it821x_port_start,
  579. };
  580. static struct ata_port_operations it821x_passthru_port_ops = {
  581. .port_disable = ata_port_disable,
  582. .set_piomode = it821x_passthru_set_piomode,
  583. .set_dmamode = it821x_passthru_set_dmamode,
  584. .mode_filter = ata_pci_default_filter,
  585. .tf_load = ata_tf_load,
  586. .tf_read = ata_tf_read,
  587. .check_status = ata_check_status,
  588. .exec_command = ata_exec_command,
  589. .check_atapi_dma= it821x_check_atapi_dma,
  590. .dev_select = it821x_passthru_dev_select,
  591. .freeze = ata_bmdma_freeze,
  592. .thaw = ata_bmdma_thaw,
  593. .error_handler = ata_bmdma_error_handler,
  594. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  595. .cable_detect = ata_cable_unknown,
  596. .bmdma_setup = ata_bmdma_setup,
  597. .bmdma_start = it821x_passthru_bmdma_start,
  598. .bmdma_stop = it821x_passthru_bmdma_stop,
  599. .bmdma_status = ata_bmdma_status,
  600. .qc_prep = ata_qc_prep,
  601. .qc_issue = it821x_passthru_qc_issue_prot,
  602. .data_xfer = ata_data_xfer,
  603. .irq_clear = ata_bmdma_irq_clear,
  604. .irq_handler = ata_interrupt,
  605. .irq_on = ata_irq_on,
  606. .irq_ack = ata_irq_ack,
  607. .port_start = it821x_port_start,
  608. };
  609. static void __devinit it821x_disable_raid(struct pci_dev *pdev)
  610. {
  611. /* Reset local CPU, and set BIOS not ready */
  612. pci_write_config_byte(pdev, 0x5E, 0x01);
  613. /* Set to bypass mode, and reset PCI bus */
  614. pci_write_config_byte(pdev, 0x50, 0x00);
  615. pci_write_config_word(pdev, PCI_COMMAND,
  616. PCI_COMMAND_PARITY | PCI_COMMAND_IO |
  617. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  618. pci_write_config_word(pdev, 0x40, 0xA0F3);
  619. pci_write_config_dword(pdev,0x4C, 0x02040204);
  620. pci_write_config_byte(pdev, 0x42, 0x36);
  621. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
  622. }
  623. static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  624. {
  625. u8 conf;
  626. static const struct ata_port_info info_smart = {
  627. .sht = &it821x_sht,
  628. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  629. .pio_mask = 0x1f,
  630. .mwdma_mask = 0x07,
  631. .port_ops = &it821x_smart_port_ops
  632. };
  633. static const struct ata_port_info info_passthru = {
  634. .sht = &it821x_sht,
  635. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  636. .pio_mask = 0x1f,
  637. .mwdma_mask = 0x07,
  638. .udma_mask = 0x7f,
  639. .port_ops = &it821x_passthru_port_ops
  640. };
  641. const struct ata_port_info *ppi[] = { NULL, NULL };
  642. static char *mode[2] = { "pass through", "smart" };
  643. /* Force the card into bypass mode if so requested */
  644. if (it8212_noraid) {
  645. printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n");
  646. it821x_disable_raid(pdev);
  647. }
  648. pci_read_config_byte(pdev, 0x50, &conf);
  649. conf &= 1;
  650. printk(KERN_INFO DRV_NAME ": controller in %s mode.\n", mode[conf]);
  651. if (conf == 0)
  652. ppi[0] = &info_passthru;
  653. else
  654. ppi[0] = &info_smart;
  655. return ata_pci_init_one(pdev, ppi);
  656. }
  657. #ifdef CONFIG_PM
  658. static int it821x_reinit_one(struct pci_dev *pdev)
  659. {
  660. /* Resume - turn raid back off if need be */
  661. if (it8212_noraid)
  662. it821x_disable_raid(pdev);
  663. return ata_pci_device_resume(pdev);
  664. }
  665. #endif
  666. static const struct pci_device_id it821x[] = {
  667. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
  668. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
  669. { },
  670. };
  671. static struct pci_driver it821x_pci_driver = {
  672. .name = DRV_NAME,
  673. .id_table = it821x,
  674. .probe = it821x_init_one,
  675. .remove = ata_pci_remove_one,
  676. #ifdef CONFIG_PM
  677. .suspend = ata_pci_device_suspend,
  678. .resume = it821x_reinit_one,
  679. #endif
  680. };
  681. static int __init it821x_init(void)
  682. {
  683. return pci_register_driver(&it821x_pci_driver);
  684. }
  685. static void __exit it821x_exit(void)
  686. {
  687. pci_unregister_driver(&it821x_pci_driver);
  688. }
  689. MODULE_AUTHOR("Alan Cox");
  690. MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
  691. MODULE_LICENSE("GPL");
  692. MODULE_DEVICE_TABLE(pci, it821x);
  693. MODULE_VERSION(DRV_VERSION);
  694. module_param_named(noraid, it8212_noraid, int, S_IRUGO);
  695. MODULE_PARM_DESC(it8212_noraid, "Force card into bypass mode");
  696. module_init(it821x_init);
  697. module_exit(it821x_exit);