alim15x3.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * Copyright (C) 1998-2000 Michel Aubry, Maintainer
  3. * Copyright (C) 1998-2000 Andrzej Krzysztofowicz, Maintainer
  4. * Copyright (C) 1999-2000 CJ, cjtsai@ali.com.tw, Maintainer
  5. *
  6. * Copyright (C) 1998-2000 Andre Hedrick (andre@linux-ide.org)
  7. * May be copied or modified under the terms of the GNU General Public License
  8. * Copyright (C) 2002 Alan Cox
  9. * ALi (now ULi M5228) support by Clear Zhang <Clear.Zhang@ali.com.tw>
  10. * Copyright (C) 2007 MontaVista Software, Inc. <source@mvista.com>
  11. * Copyright (C) 2007-2010 Bartlomiej Zolnierkiewicz
  12. *
  13. * (U)DMA capable version of ali 1533/1543(C), 1535(D)
  14. *
  15. **********************************************************************
  16. * 9/7/99 --Parts from the above author are included and need to be
  17. * converted into standard interface, once I finish the thought.
  18. *
  19. * Recent changes
  20. * Don't use LBA48 mode on ALi <= 0xC4
  21. * Don't poke 0x79 with a non ALi northbridge
  22. * Don't flip undefined bits on newer chipsets (fix Fujitsu laptop hang)
  23. * Allow UDMA6 on revisions > 0xC4
  24. *
  25. * Documentation
  26. * Chipset documentation available under NDA only
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/types.h>
  31. #include <linux/kernel.h>
  32. #include <linux/pci.h>
  33. #include <linux/ide.h>
  34. #include <linux/init.h>
  35. #include <linux/dmi.h>
  36. #include <asm/io.h>
  37. #define DRV_NAME "alim15x3"
  38. /*
  39. * ALi devices are not plug in. Otherwise these static values would
  40. * need to go. They ought to go away anyway
  41. */
  42. static u8 m5229_revision;
  43. static u8 chip_is_1543c_e;
  44. static struct pci_dev *isa_dev;
  45. static void ali_fifo_control(ide_hwif_t *hwif, ide_drive_t *drive, int on)
  46. {
  47. struct pci_dev *pdev = to_pci_dev(hwif->dev);
  48. int pio_fifo = 0x54 + hwif->channel;
  49. u8 fifo;
  50. int shift = 4 * (drive->dn & 1);
  51. pci_read_config_byte(pdev, pio_fifo, &fifo);
  52. fifo &= ~(0x0F << shift);
  53. fifo |= (on << shift);
  54. pci_write_config_byte(pdev, pio_fifo, fifo);
  55. }
  56. /**
  57. * ali_set_pio_mode - set host controller for PIO mode
  58. * @drive: drive
  59. * @pio: PIO mode number
  60. *
  61. * Program the controller for the given PIO mode.
  62. */
  63. static void ali_set_pio_mode(ide_drive_t *drive, const u8 pio)
  64. {
  65. ide_hwif_t *hwif = drive->hwif;
  66. struct pci_dev *dev = to_pci_dev(hwif->dev);
  67. int bus_speed = ide_pci_clk ? ide_pci_clk : 33;
  68. unsigned long T = 1000000 / bus_speed; /* PCI clock based */
  69. int port = hwif->channel ? 0x5c : 0x58;
  70. u8 unit = drive->dn & 1;
  71. struct ide_timing t;
  72. ide_timing_compute(drive, XFER_PIO_0 + pio, &t, T, 1);
  73. t.setup = clamp_val(t.setup, 1, 8) & 7;
  74. t.active = clamp_val(t.active, 1, 8) & 7;
  75. t.recover = clamp_val(t.recover, 1, 16) & 15;
  76. /*
  77. * PIO mode => ATA FIFO on, ATAPI FIFO off
  78. */
  79. ali_fifo_control(hwif, drive, (drive->media == ide_disk) ? 0x05 : 0x00);
  80. pci_write_config_byte(dev, port, t.setup);
  81. pci_write_config_byte(dev, port + unit + 2,
  82. (t.active << 4) | t.recover);
  83. }
  84. /**
  85. * ali_udma_filter - compute UDMA mask
  86. * @drive: IDE device
  87. *
  88. * Return available UDMA modes.
  89. *
  90. * The actual rules for the ALi are:
  91. * No UDMA on revisions <= 0x20
  92. * Disk only for revisions < 0xC2
  93. * Not WDC drives on M1543C-E (?)
  94. */
  95. static u8 ali_udma_filter(ide_drive_t *drive)
  96. {
  97. if (m5229_revision > 0x20 && m5229_revision < 0xC2) {
  98. if (drive->media != ide_disk)
  99. return 0;
  100. if (chip_is_1543c_e &&
  101. strstr((char *)&drive->id[ATA_ID_PROD], "WDC "))
  102. return 0;
  103. }
  104. return drive->hwif->ultra_mask;
  105. }
  106. /**
  107. * ali_set_dma_mode - set host controller for DMA mode
  108. * @drive: drive
  109. * @speed: DMA mode
  110. *
  111. * Configure the hardware for the desired IDE transfer mode.
  112. */
  113. static void ali_set_dma_mode(ide_drive_t *drive, const u8 speed)
  114. {
  115. ide_hwif_t *hwif = drive->hwif;
  116. struct pci_dev *dev = to_pci_dev(hwif->dev);
  117. u8 speed1 = speed;
  118. u8 unit = drive->dn & 1;
  119. u8 tmpbyte = 0x00;
  120. int m5229_udma = (hwif->channel) ? 0x57 : 0x56;
  121. if (speed == XFER_UDMA_6)
  122. speed1 = 0x47;
  123. if (speed < XFER_UDMA_0) {
  124. u8 ultra_enable = (unit) ? 0x7f : 0xf7;
  125. /*
  126. * clear "ultra enable" bit
  127. */
  128. pci_read_config_byte(dev, m5229_udma, &tmpbyte);
  129. tmpbyte &= ultra_enable;
  130. pci_write_config_byte(dev, m5229_udma, tmpbyte);
  131. /*
  132. * FIXME: Oh, my... DMA timings are never set.
  133. */
  134. } else {
  135. pci_read_config_byte(dev, m5229_udma, &tmpbyte);
  136. tmpbyte &= (0x0f << ((1-unit) << 2));
  137. /*
  138. * enable ultra dma and set timing
  139. */
  140. tmpbyte |= ((0x08 | ((4-speed1)&0x07)) << (unit << 2));
  141. pci_write_config_byte(dev, m5229_udma, tmpbyte);
  142. if (speed >= XFER_UDMA_3) {
  143. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  144. tmpbyte |= 1;
  145. pci_write_config_byte(dev, 0x4b, tmpbyte);
  146. }
  147. }
  148. }
  149. /**
  150. * ali_dma_check - DMA check
  151. * @drive: target device
  152. * @cmd: command
  153. *
  154. * Returns 1 if the DMA cannot be performed, zero on success.
  155. */
  156. static int ali_dma_check(ide_drive_t *drive, struct ide_cmd *cmd)
  157. {
  158. if (m5229_revision < 0xC2 && drive->media != ide_disk) {
  159. if (cmd->tf_flags & IDE_TFLAG_WRITE)
  160. return 1; /* try PIO instead of DMA */
  161. }
  162. return 0;
  163. }
  164. /**
  165. * init_chipset_ali15x3 - Initialise an ALi IDE controller
  166. * @dev: PCI device
  167. *
  168. * This function initializes the ALI IDE controller and where
  169. * appropriate also sets up the 1533 southbridge.
  170. */
  171. static int init_chipset_ali15x3(struct pci_dev *dev)
  172. {
  173. unsigned long flags;
  174. u8 tmpbyte;
  175. struct pci_dev *north = pci_get_slot(dev->bus, PCI_DEVFN(0,0));
  176. m5229_revision = dev->revision;
  177. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  178. local_irq_save(flags);
  179. if (m5229_revision < 0xC2) {
  180. /*
  181. * revision 0x20 (1543-E, 1543-F)
  182. * revision 0xC0, 0xC1 (1543C-C, 1543C-D, 1543C-E)
  183. * clear CD-ROM DMA write bit, m5229, 0x4b, bit 7
  184. */
  185. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  186. /*
  187. * clear bit 7
  188. */
  189. pci_write_config_byte(dev, 0x4b, tmpbyte & 0x7F);
  190. /*
  191. * check m1533, 0x5e, bit 1~4 == 1001 => & 00011110 = 00010010
  192. */
  193. if (m5229_revision >= 0x20 && isa_dev) {
  194. pci_read_config_byte(isa_dev, 0x5e, &tmpbyte);
  195. chip_is_1543c_e = ((tmpbyte & 0x1e) == 0x12) ? 1: 0;
  196. }
  197. goto out;
  198. }
  199. /*
  200. * 1543C-B?, 1535, 1535D, 1553
  201. * Note 1: not all "motherboard" support this detection
  202. * Note 2: if no udma 66 device, the detection may "error".
  203. * but in this case, we will not set the device to
  204. * ultra 66, the detection result is not important
  205. */
  206. /*
  207. * enable "Cable Detection", m5229, 0x4b, bit3
  208. */
  209. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  210. pci_write_config_byte(dev, 0x4b, tmpbyte | 0x08);
  211. /*
  212. * We should only tune the 1533 enable if we are using an ALi
  213. * North bridge. We might have no north found on some zany
  214. * box without a device at 0:0.0. The ALi bridge will be at
  215. * 0:0.0 so if we didn't find one we know what is cooking.
  216. */
  217. if (north && north->vendor != PCI_VENDOR_ID_AL)
  218. goto out;
  219. if (m5229_revision < 0xC5 && isa_dev)
  220. {
  221. /*
  222. * set south-bridge's enable bit, m1533, 0x79
  223. */
  224. pci_read_config_byte(isa_dev, 0x79, &tmpbyte);
  225. if (m5229_revision == 0xC2) {
  226. /*
  227. * 1543C-B0 (m1533, 0x79, bit 2)
  228. */
  229. pci_write_config_byte(isa_dev, 0x79, tmpbyte | 0x04);
  230. } else if (m5229_revision >= 0xC3) {
  231. /*
  232. * 1553/1535 (m1533, 0x79, bit 1)
  233. */
  234. pci_write_config_byte(isa_dev, 0x79, tmpbyte | 0x02);
  235. }
  236. }
  237. out:
  238. /*
  239. * CD_ROM DMA on (m5229, 0x53, bit0)
  240. * Enable this bit even if we want to use PIO.
  241. * PIO FIFO off (m5229, 0x53, bit1)
  242. * The hardware will use 0x54h and 0x55h to control PIO FIFO.
  243. * (Not on later devices it seems)
  244. *
  245. * 0x53 changes meaning on later revs - we must no touch
  246. * bit 1 on them. Need to check if 0x20 is the right break.
  247. */
  248. if (m5229_revision >= 0x20) {
  249. pci_read_config_byte(dev, 0x53, &tmpbyte);
  250. if (m5229_revision <= 0x20)
  251. tmpbyte = (tmpbyte & (~0x02)) | 0x01;
  252. else if (m5229_revision == 0xc7 || m5229_revision == 0xc8)
  253. tmpbyte |= 0x03;
  254. else
  255. tmpbyte |= 0x01;
  256. pci_write_config_byte(dev, 0x53, tmpbyte);
  257. }
  258. pci_dev_put(north);
  259. pci_dev_put(isa_dev);
  260. local_irq_restore(flags);
  261. return 0;
  262. }
  263. /*
  264. * Cable special cases
  265. */
  266. static const struct dmi_system_id cable_dmi_table[] = {
  267. {
  268. .ident = "HP Pavilion N5430",
  269. .matches = {
  270. DMI_MATCH(DMI_BOARD_VENDOR, "Hewlett-Packard"),
  271. DMI_MATCH(DMI_BOARD_VERSION, "OmniBook N32N-736"),
  272. },
  273. },
  274. {
  275. .ident = "Toshiba Satellite S1800-814",
  276. .matches = {
  277. DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
  278. DMI_MATCH(DMI_PRODUCT_NAME, "S1800-814"),
  279. },
  280. },
  281. { }
  282. };
  283. static int ali_cable_override(struct pci_dev *pdev)
  284. {
  285. /* Fujitsu P2000 */
  286. if (pdev->subsystem_vendor == 0x10CF &&
  287. pdev->subsystem_device == 0x10AF)
  288. return 1;
  289. /* Mitac 8317 (Winbook-A) and relatives */
  290. if (pdev->subsystem_vendor == 0x1071 &&
  291. pdev->subsystem_device == 0x8317)
  292. return 1;
  293. /* Systems by DMI */
  294. if (dmi_check_system(cable_dmi_table))
  295. return 1;
  296. return 0;
  297. }
  298. /**
  299. * ali_cable_detect - cable detection
  300. * @hwif: IDE interface
  301. *
  302. * This checks if the controller and the cable are capable
  303. * of UDMA66 transfers. It doesn't check the drives.
  304. * But see note 2 below!
  305. *
  306. * FIXME: frobs bits that are not defined on newer ALi devicea
  307. */
  308. static u8 ali_cable_detect(ide_hwif_t *hwif)
  309. {
  310. struct pci_dev *dev = to_pci_dev(hwif->dev);
  311. unsigned long flags;
  312. u8 cbl = ATA_CBL_PATA40, tmpbyte;
  313. local_irq_save(flags);
  314. if (m5229_revision >= 0xC2) {
  315. /*
  316. * m5229 80-pin cable detection (from Host View)
  317. *
  318. * 0x4a bit0 is 0 => primary channel has 80-pin
  319. * 0x4a bit1 is 0 => secondary channel has 80-pin
  320. *
  321. * Certain laptops use short but suitable cables
  322. * and don't implement the detect logic.
  323. */
  324. if (ali_cable_override(dev))
  325. cbl = ATA_CBL_PATA40_SHORT;
  326. else {
  327. pci_read_config_byte(dev, 0x4a, &tmpbyte);
  328. if ((tmpbyte & (1 << hwif->channel)) == 0)
  329. cbl = ATA_CBL_PATA80;
  330. }
  331. }
  332. local_irq_restore(flags);
  333. return cbl;
  334. }
  335. #ifndef CONFIG_SPARC64
  336. /**
  337. * init_hwif_ali15x3 - Initialize the ALI IDE x86 stuff
  338. * @hwif: interface to configure
  339. *
  340. * Obtain the IRQ tables for an ALi based IDE solution on the PC
  341. * class platforms. This part of the code isn't applicable to the
  342. * Sparc systems.
  343. */
  344. static void __devinit init_hwif_ali15x3 (ide_hwif_t *hwif)
  345. {
  346. u8 ideic, inmir;
  347. s8 irq_routing_table[] = { -1, 9, 3, 10, 4, 5, 7, 6,
  348. 1, 11, 0, 12, 0, 14, 0, 15 };
  349. int irq = -1;
  350. if (isa_dev) {
  351. /*
  352. * read IDE interface control
  353. */
  354. pci_read_config_byte(isa_dev, 0x58, &ideic);
  355. /* bit0, bit1 */
  356. ideic = ideic & 0x03;
  357. /* get IRQ for IDE Controller */
  358. if ((hwif->channel && ideic == 0x03) ||
  359. (!hwif->channel && !ideic)) {
  360. /*
  361. * get SIRQ1 routing table
  362. */
  363. pci_read_config_byte(isa_dev, 0x44, &inmir);
  364. inmir = inmir & 0x0f;
  365. irq = irq_routing_table[inmir];
  366. } else if (hwif->channel && !(ideic & 0x01)) {
  367. /*
  368. * get SIRQ2 routing table
  369. */
  370. pci_read_config_byte(isa_dev, 0x75, &inmir);
  371. inmir = inmir & 0x0f;
  372. irq = irq_routing_table[inmir];
  373. }
  374. if(irq >= 0)
  375. hwif->irq = irq;
  376. }
  377. }
  378. #else
  379. #define init_hwif_ali15x3 NULL
  380. #endif /* CONFIG_SPARC64 */
  381. /**
  382. * init_dma_ali15x3 - set up DMA on ALi15x3
  383. * @hwif: IDE interface
  384. * @d: IDE port info
  385. *
  386. * Set up the DMA functionality on the ALi 15x3.
  387. */
  388. static int __devinit init_dma_ali15x3(ide_hwif_t *hwif,
  389. const struct ide_port_info *d)
  390. {
  391. struct pci_dev *dev = to_pci_dev(hwif->dev);
  392. unsigned long base = ide_pci_dma_base(hwif, d);
  393. if (base == 0)
  394. return -1;
  395. hwif->dma_base = base;
  396. if (ide_pci_check_simplex(hwif, d) < 0)
  397. return -1;
  398. if (ide_pci_set_master(dev, d->name) < 0)
  399. return -1;
  400. if (!hwif->channel)
  401. outb(inb(base + 2) & 0x60, base + 2);
  402. printk(KERN_INFO " %s: BM-DMA at 0x%04lx-0x%04lx\n",
  403. hwif->name, base, base + 7);
  404. if (ide_allocate_dma_engine(hwif))
  405. return -1;
  406. return 0;
  407. }
  408. static const struct ide_port_ops ali_port_ops = {
  409. .set_pio_mode = ali_set_pio_mode,
  410. .set_dma_mode = ali_set_dma_mode,
  411. .udma_filter = ali_udma_filter,
  412. .cable_detect = ali_cable_detect,
  413. };
  414. static const struct ide_dma_ops ali_dma_ops = {
  415. .dma_host_set = ide_dma_host_set,
  416. .dma_setup = ide_dma_setup,
  417. .dma_start = ide_dma_start,
  418. .dma_end = ide_dma_end,
  419. .dma_test_irq = ide_dma_test_irq,
  420. .dma_lost_irq = ide_dma_lost_irq,
  421. .dma_check = ali_dma_check,
  422. .dma_timer_expiry = ide_dma_sff_timer_expiry,
  423. .dma_sff_read_status = ide_dma_sff_read_status,
  424. };
  425. static const struct ide_port_info ali15x3_chipset __devinitdata = {
  426. .name = DRV_NAME,
  427. .init_chipset = init_chipset_ali15x3,
  428. .init_hwif = init_hwif_ali15x3,
  429. .init_dma = init_dma_ali15x3,
  430. .port_ops = &ali_port_ops,
  431. .dma_ops = &sff_dma_ops,
  432. .pio_mask = ATA_PIO5,
  433. .swdma_mask = ATA_SWDMA2,
  434. .mwdma_mask = ATA_MWDMA2,
  435. };
  436. /**
  437. * alim15x3_init_one - set up an ALi15x3 IDE controller
  438. * @dev: PCI device to set up
  439. *
  440. * Perform the actual set up for an ALi15x3 that has been found by the
  441. * hot plug layer.
  442. */
  443. static int __devinit alim15x3_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  444. {
  445. struct ide_port_info d = ali15x3_chipset;
  446. u8 rev = dev->revision, idx = id->driver_data;
  447. /* don't use LBA48 DMA on ALi devices before rev 0xC5 */
  448. if (rev <= 0xC4)
  449. d.host_flags |= IDE_HFLAG_NO_LBA48_DMA;
  450. if (rev >= 0x20) {
  451. if (rev == 0x20)
  452. d.host_flags |= IDE_HFLAG_NO_ATAPI_DMA;
  453. if (rev < 0xC2)
  454. d.udma_mask = ATA_UDMA2;
  455. else if (rev == 0xC2 || rev == 0xC3)
  456. d.udma_mask = ATA_UDMA4;
  457. else if (rev == 0xC4)
  458. d.udma_mask = ATA_UDMA5;
  459. else
  460. d.udma_mask = ATA_UDMA6;
  461. d.dma_ops = &ali_dma_ops;
  462. } else {
  463. d.host_flags |= IDE_HFLAG_NO_DMA;
  464. d.mwdma_mask = d.swdma_mask = 0;
  465. }
  466. if (idx == 0)
  467. d.host_flags |= IDE_HFLAG_CLEAR_SIMPLEX;
  468. return ide_pci_init_one(dev, &d, NULL);
  469. }
  470. static const struct pci_device_id alim15x3_pci_tbl[] = {
  471. { PCI_VDEVICE(AL, PCI_DEVICE_ID_AL_M5229), 0 },
  472. { PCI_VDEVICE(AL, PCI_DEVICE_ID_AL_M5228), 1 },
  473. { 0, },
  474. };
  475. MODULE_DEVICE_TABLE(pci, alim15x3_pci_tbl);
  476. static struct pci_driver alim15x3_pci_driver = {
  477. .name = "ALI15x3_IDE",
  478. .id_table = alim15x3_pci_tbl,
  479. .probe = alim15x3_init_one,
  480. .remove = ide_pci_remove,
  481. .suspend = ide_pci_suspend,
  482. .resume = ide_pci_resume,
  483. };
  484. static int __init ali15x3_ide_init(void)
  485. {
  486. return ide_pci_register_driver(&alim15x3_pci_driver);
  487. }
  488. static void __exit ali15x3_ide_exit(void)
  489. {
  490. pci_unregister_driver(&alim15x3_pci_driver);
  491. }
  492. module_init(ali15x3_ide_init);
  493. module_exit(ali15x3_ide_exit);
  494. MODULE_AUTHOR("Michael Aubry, Andrzej Krzysztofowicz, CJ, Andre Hedrick, Alan Cox, Bartlomiej Zolnierkiewicz");
  495. MODULE_DESCRIPTION("PCI driver module for ALi 15x3 IDE");
  496. MODULE_LICENSE("GPL");