alim15x3.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /*
  2. * linux/drivers/ide/pci/alim15x3.c Version 0.21 2007/02/03
  3. *
  4. * Copyright (C) 1998-2000 Michel Aubry, Maintainer
  5. * Copyright (C) 1998-2000 Andrzej Krzysztofowicz, Maintainer
  6. * Copyright (C) 1999-2000 CJ, cjtsai@ali.com.tw, Maintainer
  7. *
  8. * Copyright (C) 1998-2000 Andre Hedrick (andre@linux-ide.org)
  9. * May be copied or modified under the terms of the GNU General Public License
  10. * Copyright (C) 2002 Alan Cox <alan@redhat.com>
  11. * ALi (now ULi M5228) support by Clear Zhang <Clear.Zhang@ali.com.tw>
  12. * Copyright (C) 2007 MontaVista Software, Inc. <source@mvista.com>
  13. *
  14. * (U)DMA capable version of ali 1533/1543(C), 1535(D)
  15. *
  16. **********************************************************************
  17. * 9/7/99 --Parts from the above author are included and need to be
  18. * converted into standard interface, once I finish the thought.
  19. *
  20. * Recent changes
  21. * Don't use LBA48 mode on ALi <= 0xC4
  22. * Don't poke 0x79 with a non ALi northbridge
  23. * Don't flip undefined bits on newer chipsets (fix Fujitsu laptop hang)
  24. * Allow UDMA6 on revisions > 0xC4
  25. *
  26. * Documentation
  27. * Chipset documentation available under NDA only
  28. *
  29. */
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/kernel.h>
  33. #include <linux/pci.h>
  34. #include <linux/delay.h>
  35. #include <linux/hdreg.h>
  36. #include <linux/ide.h>
  37. #include <linux/init.h>
  38. #include <asm/io.h>
  39. #define DISPLAY_ALI_TIMINGS
  40. /*
  41. * ALi devices are not plug in. Otherwise these static values would
  42. * need to go. They ought to go away anyway
  43. */
  44. static u8 m5229_revision;
  45. static u8 chip_is_1543c_e;
  46. static struct pci_dev *isa_dev;
  47. #if defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS)
  48. #include <linux/stat.h>
  49. #include <linux/proc_fs.h>
  50. static u8 ali_proc = 0;
  51. static struct pci_dev *bmide_dev;
  52. static char *fifo[4] = {
  53. "FIFO Off",
  54. "FIFO On ",
  55. "DMA mode",
  56. "PIO mode" };
  57. static char *udmaT[8] = {
  58. "1.5T",
  59. " 2T",
  60. "2.5T",
  61. " 3T",
  62. "3.5T",
  63. " 4T",
  64. " 6T",
  65. " 8T"
  66. };
  67. static char *channel_status[8] = {
  68. "OK ",
  69. "busy ",
  70. "DRQ ",
  71. "DRQ busy ",
  72. "error ",
  73. "error busy ",
  74. "error DRQ ",
  75. "error DRQ busy"
  76. };
  77. /**
  78. * ali_get_info - generate proc file for ALi IDE
  79. * @buffer: buffer to fill
  80. * @addr: address of user start in buffer
  81. * @offset: offset into 'file'
  82. * @count: buffer count
  83. *
  84. * Walks the Ali devices and outputs summary data on the tuning and
  85. * anything else that will help with debugging
  86. */
  87. static int ali_get_info (char *buffer, char **addr, off_t offset, int count)
  88. {
  89. unsigned long bibma;
  90. u8 reg53h, reg5xh, reg5yh, reg5xh1, reg5yh1, c0, c1, rev, tmp;
  91. char *q, *p = buffer;
  92. /* fetch rev. */
  93. pci_read_config_byte(bmide_dev, 0x08, &rev);
  94. if (rev >= 0xc1) /* M1543C or newer */
  95. udmaT[7] = " ???";
  96. else
  97. fifo[3] = " ??? ";
  98. /* first fetch bibma: */
  99. bibma = pci_resource_start(bmide_dev, 4);
  100. /*
  101. * at that point bibma+0x2 et bibma+0xa are byte
  102. * registers to investigate:
  103. */
  104. c0 = inb(bibma + 0x02);
  105. c1 = inb(bibma + 0x0a);
  106. p += sprintf(p,
  107. "\n Ali M15x3 Chipset.\n");
  108. p += sprintf(p,
  109. " ------------------\n");
  110. pci_read_config_byte(bmide_dev, 0x78, &reg53h);
  111. p += sprintf(p, "PCI Clock: %d.\n", reg53h);
  112. pci_read_config_byte(bmide_dev, 0x53, &reg53h);
  113. p += sprintf(p,
  114. "CD_ROM FIFO:%s, CD_ROM DMA:%s\n",
  115. (reg53h & 0x02) ? "Yes" : "No ",
  116. (reg53h & 0x01) ? "Yes" : "No " );
  117. pci_read_config_byte(bmide_dev, 0x74, &reg53h);
  118. p += sprintf(p,
  119. "FIFO Status: contains %d Words, runs%s%s\n\n",
  120. (reg53h & 0x3f),
  121. (reg53h & 0x40) ? " OVERWR" : "",
  122. (reg53h & 0x80) ? " OVERRD." : "." );
  123. p += sprintf(p,
  124. "-------------------primary channel"
  125. "-------------------secondary channel"
  126. "---------\n\n");
  127. pci_read_config_byte(bmide_dev, 0x09, &reg53h);
  128. p += sprintf(p,
  129. "channel status: %s"
  130. " %s\n",
  131. (reg53h & 0x20) ? "On " : "Off",
  132. (reg53h & 0x10) ? "On " : "Off" );
  133. p += sprintf(p,
  134. "both channels togth: %s"
  135. " %s\n",
  136. (c0&0x80) ? "No " : "Yes",
  137. (c1&0x80) ? "No " : "Yes" );
  138. pci_read_config_byte(bmide_dev, 0x76, &reg53h);
  139. p += sprintf(p,
  140. "Channel state: %s %s\n",
  141. channel_status[reg53h & 0x07],
  142. channel_status[(reg53h & 0x70) >> 4] );
  143. pci_read_config_byte(bmide_dev, 0x58, &reg5xh);
  144. pci_read_config_byte(bmide_dev, 0x5c, &reg5yh);
  145. p += sprintf(p,
  146. "Add. Setup Timing: %dT"
  147. " %dT\n",
  148. (reg5xh & 0x07) ? (reg5xh & 0x07) : 8,
  149. (reg5yh & 0x07) ? (reg5yh & 0x07) : 8 );
  150. pci_read_config_byte(bmide_dev, 0x59, &reg5xh);
  151. pci_read_config_byte(bmide_dev, 0x5d, &reg5yh);
  152. p += sprintf(p,
  153. "Command Act. Count: %dT"
  154. " %dT\n"
  155. "Command Rec. Count: %dT"
  156. " %dT\n\n",
  157. (reg5xh & 0x70) ? ((reg5xh & 0x70) >> 4) : 8,
  158. (reg5yh & 0x70) ? ((reg5yh & 0x70) >> 4) : 8,
  159. (reg5xh & 0x0f) ? (reg5xh & 0x0f) : 16,
  160. (reg5yh & 0x0f) ? (reg5yh & 0x0f) : 16 );
  161. p += sprintf(p,
  162. "----------------drive0-----------drive1"
  163. "------------drive0-----------drive1------\n\n");
  164. p += sprintf(p,
  165. "DMA enabled: %s %s"
  166. " %s %s\n",
  167. (c0&0x20) ? "Yes" : "No ",
  168. (c0&0x40) ? "Yes" : "No ",
  169. (c1&0x20) ? "Yes" : "No ",
  170. (c1&0x40) ? "Yes" : "No " );
  171. pci_read_config_byte(bmide_dev, 0x54, &reg5xh);
  172. pci_read_config_byte(bmide_dev, 0x55, &reg5yh);
  173. q = "FIFO threshold: %2d Words %2d Words"
  174. " %2d Words %2d Words\n";
  175. if (rev < 0xc1) {
  176. if ((rev == 0x20) &&
  177. (pci_read_config_byte(bmide_dev, 0x4f, &tmp), (tmp &= 0x20))) {
  178. p += sprintf(p, q, 8, 8, 8, 8);
  179. } else {
  180. p += sprintf(p, q,
  181. (reg5xh & 0x03) + 12,
  182. ((reg5xh & 0x30)>>4) + 12,
  183. (reg5yh & 0x03) + 12,
  184. ((reg5yh & 0x30)>>4) + 12 );
  185. }
  186. } else {
  187. int t1 = (tmp = (reg5xh & 0x03)) ? (tmp << 3) : 4;
  188. int t2 = (tmp = ((reg5xh & 0x30)>>4)) ? (tmp << 3) : 4;
  189. int t3 = (tmp = (reg5yh & 0x03)) ? (tmp << 3) : 4;
  190. int t4 = (tmp = ((reg5yh & 0x30)>>4)) ? (tmp << 3) : 4;
  191. p += sprintf(p, q, t1, t2, t3, t4);
  192. }
  193. #if 0
  194. p += sprintf(p,
  195. "FIFO threshold: %2d Words %2d Words"
  196. " %2d Words %2d Words\n",
  197. (reg5xh & 0x03) + 12,
  198. ((reg5xh & 0x30)>>4) + 12,
  199. (reg5yh & 0x03) + 12,
  200. ((reg5yh & 0x30)>>4) + 12 );
  201. #endif
  202. p += sprintf(p,
  203. "FIFO mode: %s %s %s %s\n",
  204. fifo[((reg5xh & 0x0c) >> 2)],
  205. fifo[((reg5xh & 0xc0) >> 6)],
  206. fifo[((reg5yh & 0x0c) >> 2)],
  207. fifo[((reg5yh & 0xc0) >> 6)] );
  208. pci_read_config_byte(bmide_dev, 0x5a, &reg5xh);
  209. pci_read_config_byte(bmide_dev, 0x5b, &reg5xh1);
  210. pci_read_config_byte(bmide_dev, 0x5e, &reg5yh);
  211. pci_read_config_byte(bmide_dev, 0x5f, &reg5yh1);
  212. p += sprintf(p,/*
  213. "------------------drive0-----------drive1"
  214. "------------drive0-----------drive1------\n")*/
  215. "Dt RW act. Cnt %2dT %2dT"
  216. " %2dT %2dT\n"
  217. "Dt RW rec. Cnt %2dT %2dT"
  218. " %2dT %2dT\n\n",
  219. (reg5xh & 0x70) ? ((reg5xh & 0x70) >> 4) : 8,
  220. (reg5xh1 & 0x70) ? ((reg5xh1 & 0x70) >> 4) : 8,
  221. (reg5yh & 0x70) ? ((reg5yh & 0x70) >> 4) : 8,
  222. (reg5yh1 & 0x70) ? ((reg5yh1 & 0x70) >> 4) : 8,
  223. (reg5xh & 0x0f) ? (reg5xh & 0x0f) : 16,
  224. (reg5xh1 & 0x0f) ? (reg5xh1 & 0x0f) : 16,
  225. (reg5yh & 0x0f) ? (reg5yh & 0x0f) : 16,
  226. (reg5yh1 & 0x0f) ? (reg5yh1 & 0x0f) : 16 );
  227. p += sprintf(p,
  228. "-----------------------------------UDMA Timings"
  229. "--------------------------------\n\n");
  230. pci_read_config_byte(bmide_dev, 0x56, &reg5xh);
  231. pci_read_config_byte(bmide_dev, 0x57, &reg5yh);
  232. p += sprintf(p,
  233. "UDMA: %s %s"
  234. " %s %s\n"
  235. "UDMA timings: %s %s"
  236. " %s %s\n\n",
  237. (reg5xh & 0x08) ? "OK" : "No",
  238. (reg5xh & 0x80) ? "OK" : "No",
  239. (reg5yh & 0x08) ? "OK" : "No",
  240. (reg5yh & 0x80) ? "OK" : "No",
  241. udmaT[(reg5xh & 0x07)],
  242. udmaT[(reg5xh & 0x70) >> 4],
  243. udmaT[reg5yh & 0x07],
  244. udmaT[(reg5yh & 0x70) >> 4] );
  245. return p-buffer; /* => must be less than 4k! */
  246. }
  247. #endif /* defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS) */
  248. /**
  249. * ali15x3_tune_pio - set up chipset for PIO mode
  250. * @drive: drive to tune
  251. * @pio: desired mode
  252. *
  253. * Select the best PIO mode for the drive in question.
  254. * Then program the controller for this mode.
  255. *
  256. * Returns the PIO mode programmed.
  257. */
  258. static u8 ali15x3_tune_pio (ide_drive_t *drive, u8 pio)
  259. {
  260. ide_pio_data_t d;
  261. ide_hwif_t *hwif = HWIF(drive);
  262. struct pci_dev *dev = hwif->pci_dev;
  263. int s_time, a_time, c_time;
  264. u8 s_clc, a_clc, r_clc;
  265. unsigned long flags;
  266. int bus_speed = system_bus_clock();
  267. int port = hwif->channel ? 0x5c : 0x58;
  268. int portFIFO = hwif->channel ? 0x55 : 0x54;
  269. u8 cd_dma_fifo = 0;
  270. int unit = drive->select.b.unit & 1;
  271. pio = ide_get_best_pio_mode(drive, pio, 5, &d);
  272. s_time = ide_pio_timings[pio].setup_time;
  273. a_time = ide_pio_timings[pio].active_time;
  274. if ((s_clc = (s_time * bus_speed + 999) / 1000) >= 8)
  275. s_clc = 0;
  276. if ((a_clc = (a_time * bus_speed + 999) / 1000) >= 8)
  277. a_clc = 0;
  278. c_time = ide_pio_timings[pio].cycle_time;
  279. #if 0
  280. if ((r_clc = ((c_time - s_time - a_time) * bus_speed + 999) / 1000) >= 16)
  281. r_clc = 0;
  282. #endif
  283. if (!(r_clc = (c_time * bus_speed + 999) / 1000 - a_clc - s_clc)) {
  284. r_clc = 1;
  285. } else {
  286. if (r_clc >= 16)
  287. r_clc = 0;
  288. }
  289. local_irq_save(flags);
  290. /*
  291. * PIO mode => ATA FIFO on, ATAPI FIFO off
  292. */
  293. pci_read_config_byte(dev, portFIFO, &cd_dma_fifo);
  294. if (drive->media==ide_disk) {
  295. if (unit) {
  296. pci_write_config_byte(dev, portFIFO, (cd_dma_fifo & 0x0F) | 0x50);
  297. } else {
  298. pci_write_config_byte(dev, portFIFO, (cd_dma_fifo & 0xF0) | 0x05);
  299. }
  300. } else {
  301. if (unit) {
  302. pci_write_config_byte(dev, portFIFO, cd_dma_fifo & 0x0F);
  303. } else {
  304. pci_write_config_byte(dev, portFIFO, cd_dma_fifo & 0xF0);
  305. }
  306. }
  307. pci_write_config_byte(dev, port, s_clc);
  308. pci_write_config_byte(dev, port+drive->select.b.unit+2, (a_clc << 4) | r_clc);
  309. local_irq_restore(flags);
  310. /*
  311. * setup active rec
  312. * { 70, 165, 365 }, PIO Mode 0
  313. * { 50, 125, 208 }, PIO Mode 1
  314. * { 30, 100, 110 }, PIO Mode 2
  315. * { 30, 80, 70 }, PIO Mode 3 with IORDY
  316. * { 25, 70, 25 }, PIO Mode 4 with IORDY ns
  317. * { 20, 50, 30 } PIO Mode 5 with IORDY (nonstandard)
  318. */
  319. return pio;
  320. }
  321. /**
  322. * ali15x3_tune_drive - set up drive for PIO mode
  323. * @drive: drive to tune
  324. * @pio: desired mode
  325. *
  326. * Program the controller with the best PIO timing for the given drive.
  327. * Then set up the drive itself.
  328. */
  329. static void ali15x3_tune_drive (ide_drive_t *drive, u8 pio)
  330. {
  331. pio = ali15x3_tune_pio(drive, pio);
  332. (void) ide_config_drive_speed(drive, XFER_PIO_0 + pio);
  333. }
  334. /**
  335. * ali15x3_can_ultra - check for ultra DMA support
  336. * @drive: drive to do the check
  337. *
  338. * Check the drive and controller revisions. Return 0 if UDMA is
  339. * not available, or 1 if UDMA can be used. The actual rules for
  340. * the ALi are
  341. * No UDMA on revisions <= 0x20
  342. * Disk only for revisions < 0xC2
  343. * Not WDC drives for revisions < 0xC2
  344. *
  345. * FIXME: WDC ifdef needs to die
  346. */
  347. static u8 ali15x3_can_ultra (ide_drive_t *drive)
  348. {
  349. #ifndef CONFIG_WDC_ALI15X3
  350. struct hd_driveid *id = drive->id;
  351. #endif /* CONFIG_WDC_ALI15X3 */
  352. if (m5229_revision <= 0x20) {
  353. return 0;
  354. } else if ((m5229_revision < 0xC2) &&
  355. #ifndef CONFIG_WDC_ALI15X3
  356. ((chip_is_1543c_e && strstr(id->model, "WDC ")) ||
  357. (drive->media!=ide_disk))) {
  358. #else /* CONFIG_WDC_ALI15X3 */
  359. (drive->media!=ide_disk)) {
  360. #endif /* CONFIG_WDC_ALI15X3 */
  361. return 0;
  362. } else {
  363. return 1;
  364. }
  365. }
  366. /**
  367. * ali15x3_ratemask - generate DMA mode list
  368. * @drive: drive to compute against
  369. *
  370. * Generate a list of the available DMA modes for the drive.
  371. * FIXME: this function contains lots of bogus masking we can dump
  372. *
  373. * Return the highest available mode (UDMA33, UDMA66, UDMA100,..)
  374. */
  375. static u8 ali15x3_ratemask (ide_drive_t *drive)
  376. {
  377. u8 mode = 0, can_ultra = ali15x3_can_ultra(drive);
  378. if (m5229_revision > 0xC4 && can_ultra) {
  379. mode = 4;
  380. } else if (m5229_revision == 0xC4 && can_ultra) {
  381. mode = 3;
  382. } else if (m5229_revision >= 0xC2 && can_ultra) {
  383. mode = 2;
  384. } else if (can_ultra) {
  385. return 1;
  386. } else {
  387. return 0;
  388. }
  389. /*
  390. * If the drive sees no suitable cable then UDMA 33
  391. * is the highest permitted mode
  392. */
  393. if (!eighty_ninty_three(drive))
  394. mode = min(mode, (u8)1);
  395. return mode;
  396. }
  397. /**
  398. * ali15x3_tune_chipset - set up chipset/drive for new speed
  399. * @drive: drive to configure for
  400. * @xferspeed: desired speed
  401. *
  402. * Configure the hardware for the desired IDE transfer mode.
  403. * We also do the needed drive configuration through helpers
  404. */
  405. static int ali15x3_tune_chipset (ide_drive_t *drive, u8 xferspeed)
  406. {
  407. ide_hwif_t *hwif = HWIF(drive);
  408. struct pci_dev *dev = hwif->pci_dev;
  409. u8 speed = ide_rate_filter(ali15x3_ratemask(drive), xferspeed);
  410. u8 speed1 = speed;
  411. u8 unit = (drive->select.b.unit & 0x01);
  412. u8 tmpbyte = 0x00;
  413. int m5229_udma = (hwif->channel) ? 0x57 : 0x56;
  414. if (speed == XFER_UDMA_6)
  415. speed1 = 0x47;
  416. if (speed < XFER_UDMA_0) {
  417. u8 ultra_enable = (unit) ? 0x7f : 0xf7;
  418. /*
  419. * clear "ultra enable" bit
  420. */
  421. pci_read_config_byte(dev, m5229_udma, &tmpbyte);
  422. tmpbyte &= ultra_enable;
  423. pci_write_config_byte(dev, m5229_udma, tmpbyte);
  424. if (speed < XFER_SW_DMA_0)
  425. (void) ali15x3_tune_pio(drive, speed - XFER_PIO_0);
  426. } else {
  427. pci_read_config_byte(dev, m5229_udma, &tmpbyte);
  428. tmpbyte &= (0x0f << ((1-unit) << 2));
  429. /*
  430. * enable ultra dma and set timing
  431. */
  432. tmpbyte |= ((0x08 | ((4-speed1)&0x07)) << (unit << 2));
  433. pci_write_config_byte(dev, m5229_udma, tmpbyte);
  434. if (speed >= XFER_UDMA_3) {
  435. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  436. tmpbyte |= 1;
  437. pci_write_config_byte(dev, 0x4b, tmpbyte);
  438. }
  439. }
  440. return (ide_config_drive_speed(drive, speed));
  441. }
  442. /**
  443. * config_chipset_for_dma - set up DMA mode
  444. * @drive: drive to configure for
  445. *
  446. * Place a drive into DMA mode and tune the chipset for
  447. * the selected speed.
  448. *
  449. * Returns true if DMA mode can be used
  450. */
  451. static int config_chipset_for_dma (ide_drive_t *drive)
  452. {
  453. u8 speed = ide_dma_speed(drive, ali15x3_ratemask(drive));
  454. if (!(speed))
  455. return 0;
  456. (void) ali15x3_tune_chipset(drive, speed);
  457. return ide_dma_enable(drive);
  458. }
  459. /**
  460. * ali15x3_config_drive_for_dma - configure for DMA
  461. * @drive: drive to configure
  462. *
  463. * Configure a drive for DMA operation. If DMA is not possible we
  464. * drop the drive into PIO mode instead.
  465. */
  466. static int ali15x3_config_drive_for_dma(ide_drive_t *drive)
  467. {
  468. ide_hwif_t *hwif = HWIF(drive);
  469. struct hd_driveid *id = drive->id;
  470. if ((m5229_revision<=0x20) && (drive->media!=ide_disk))
  471. goto no_dma_set;
  472. drive->init_speed = 0;
  473. if ((id != NULL) && ((id->capability & 1) != 0) && drive->autodma) {
  474. /* Consult the list of known "bad" drives */
  475. if (__ide_dma_bad_drive(drive))
  476. goto ata_pio;
  477. if ((id->field_valid & 4) && (m5229_revision >= 0xC2)) {
  478. if (id->dma_ultra & hwif->ultra_mask) {
  479. /* Force if Capable UltraDMA */
  480. int dma = config_chipset_for_dma(drive);
  481. if ((id->field_valid & 2) && !dma)
  482. goto try_dma_modes;
  483. }
  484. } else if (id->field_valid & 2) {
  485. try_dma_modes:
  486. if ((id->dma_mword & hwif->mwdma_mask) ||
  487. (id->dma_1word & hwif->swdma_mask)) {
  488. /* Force if Capable regular DMA modes */
  489. if (!config_chipset_for_dma(drive))
  490. goto no_dma_set;
  491. }
  492. } else if (__ide_dma_good_drive(drive) &&
  493. (id->eide_dma_time < 150)) {
  494. /* Consult the list of known "good" drives */
  495. if (!config_chipset_for_dma(drive))
  496. goto no_dma_set;
  497. } else {
  498. goto ata_pio;
  499. }
  500. } else {
  501. ata_pio:
  502. hwif->tuneproc(drive, 255);
  503. no_dma_set:
  504. return -1;
  505. }
  506. return 0;
  507. }
  508. /**
  509. * ali15x3_dma_setup - begin a DMA phase
  510. * @drive: target device
  511. *
  512. * Returns 1 if the DMA cannot be performed, zero on success.
  513. */
  514. static int ali15x3_dma_setup(ide_drive_t *drive)
  515. {
  516. if (m5229_revision < 0xC2 && drive->media != ide_disk) {
  517. if (rq_data_dir(drive->hwif->hwgroup->rq))
  518. return 1; /* try PIO instead of DMA */
  519. }
  520. return ide_dma_setup(drive);
  521. }
  522. /**
  523. * init_chipset_ali15x3 - Initialise an ALi IDE controller
  524. * @dev: PCI device
  525. * @name: Name of the controller
  526. *
  527. * This function initializes the ALI IDE controller and where
  528. * appropriate also sets up the 1533 southbridge.
  529. */
  530. static unsigned int __devinit init_chipset_ali15x3 (struct pci_dev *dev, const char *name)
  531. {
  532. unsigned long flags;
  533. u8 tmpbyte;
  534. struct pci_dev *north = pci_get_slot(dev->bus, PCI_DEVFN(0,0));
  535. pci_read_config_byte(dev, PCI_REVISION_ID, &m5229_revision);
  536. isa_dev = pci_get_device(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1533, NULL);
  537. #if defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS)
  538. if (!ali_proc) {
  539. ali_proc = 1;
  540. bmide_dev = dev;
  541. ide_pci_create_host_proc("ali", ali_get_info);
  542. }
  543. #endif /* defined(DISPLAY_ALI_TIMINGS) && defined(CONFIG_PROC_FS) */
  544. local_irq_save(flags);
  545. if (m5229_revision < 0xC2) {
  546. /*
  547. * revision 0x20 (1543-E, 1543-F)
  548. * revision 0xC0, 0xC1 (1543C-C, 1543C-D, 1543C-E)
  549. * clear CD-ROM DMA write bit, m5229, 0x4b, bit 7
  550. */
  551. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  552. /*
  553. * clear bit 7
  554. */
  555. pci_write_config_byte(dev, 0x4b, tmpbyte & 0x7F);
  556. goto out;
  557. }
  558. /*
  559. * 1543C-B?, 1535, 1535D, 1553
  560. * Note 1: not all "motherboard" support this detection
  561. * Note 2: if no udma 66 device, the detection may "error".
  562. * but in this case, we will not set the device to
  563. * ultra 66, the detection result is not important
  564. */
  565. /*
  566. * enable "Cable Detection", m5229, 0x4b, bit3
  567. */
  568. pci_read_config_byte(dev, 0x4b, &tmpbyte);
  569. pci_write_config_byte(dev, 0x4b, tmpbyte | 0x08);
  570. /*
  571. * We should only tune the 1533 enable if we are using an ALi
  572. * North bridge. We might have no north found on some zany
  573. * box without a device at 0:0.0. The ALi bridge will be at
  574. * 0:0.0 so if we didn't find one we know what is cooking.
  575. */
  576. if (north && north->vendor != PCI_VENDOR_ID_AL)
  577. goto out;
  578. if (m5229_revision < 0xC5 && isa_dev)
  579. {
  580. /*
  581. * set south-bridge's enable bit, m1533, 0x79
  582. */
  583. pci_read_config_byte(isa_dev, 0x79, &tmpbyte);
  584. if (m5229_revision == 0xC2) {
  585. /*
  586. * 1543C-B0 (m1533, 0x79, bit 2)
  587. */
  588. pci_write_config_byte(isa_dev, 0x79, tmpbyte | 0x04);
  589. } else if (m5229_revision >= 0xC3) {
  590. /*
  591. * 1553/1535 (m1533, 0x79, bit 1)
  592. */
  593. pci_write_config_byte(isa_dev, 0x79, tmpbyte | 0x02);
  594. }
  595. }
  596. out:
  597. pci_dev_put(north);
  598. pci_dev_put(isa_dev);
  599. local_irq_restore(flags);
  600. return 0;
  601. }
  602. /**
  603. * ata66_ali15x3 - check for UDMA 66 support
  604. * @hwif: IDE interface
  605. *
  606. * This checks if the controller and the cable are capable
  607. * of UDMA66 transfers. It doesn't check the drives.
  608. * But see note 2 below!
  609. *
  610. * FIXME: frobs bits that are not defined on newer ALi devicea
  611. */
  612. static unsigned int __devinit ata66_ali15x3 (ide_hwif_t *hwif)
  613. {
  614. struct pci_dev *dev = hwif->pci_dev;
  615. unsigned int ata66 = 0;
  616. u8 cable_80_pin[2] = { 0, 0 };
  617. unsigned long flags;
  618. u8 tmpbyte;
  619. local_irq_save(flags);
  620. if (m5229_revision >= 0xC2) {
  621. /*
  622. * Ultra66 cable detection (from Host View)
  623. * m5229, 0x4a, bit0: primary, bit1: secondary 80 pin
  624. */
  625. pci_read_config_byte(dev, 0x4a, &tmpbyte);
  626. /*
  627. * 0x4a, bit0 is 0 => primary channel
  628. * has 80-pin (from host view)
  629. */
  630. if (!(tmpbyte & 0x01)) cable_80_pin[0] = 1;
  631. /*
  632. * 0x4a, bit1 is 0 => secondary channel
  633. * has 80-pin (from host view)
  634. */
  635. if (!(tmpbyte & 0x02)) cable_80_pin[1] = 1;
  636. /*
  637. * Allow ata66 if cable of current channel has 80 pins
  638. */
  639. ata66 = (hwif->channel)?cable_80_pin[1]:cable_80_pin[0];
  640. } else {
  641. /*
  642. * check m1533, 0x5e, bit 1~4 == 1001 => & 00011110 = 00010010
  643. */
  644. pci_read_config_byte(isa_dev, 0x5e, &tmpbyte);
  645. chip_is_1543c_e = ((tmpbyte & 0x1e) == 0x12) ? 1: 0;
  646. }
  647. /*
  648. * CD_ROM DMA on (m5229, 0x53, bit0)
  649. * Enable this bit even if we want to use PIO
  650. * PIO FIFO off (m5229, 0x53, bit1)
  651. * The hardware will use 0x54h and 0x55h to control PIO FIFO
  652. * (Not on later devices it seems)
  653. *
  654. * 0x53 changes meaning on later revs - we must no touch
  655. * bit 1 on them. Need to check if 0x20 is the right break
  656. */
  657. pci_read_config_byte(dev, 0x53, &tmpbyte);
  658. if(m5229_revision <= 0x20)
  659. tmpbyte = (tmpbyte & (~0x02)) | 0x01;
  660. else if (m5229_revision == 0xc7 || m5229_revision == 0xc8)
  661. tmpbyte |= 0x03;
  662. else
  663. tmpbyte |= 0x01;
  664. pci_write_config_byte(dev, 0x53, tmpbyte);
  665. local_irq_restore(flags);
  666. return(ata66);
  667. }
  668. /**
  669. * init_hwif_common_ali15x3 - Set up ALI IDE hardware
  670. * @hwif: IDE interface
  671. *
  672. * Initialize the IDE structure side of the ALi 15x3 driver.
  673. */
  674. static void __devinit init_hwif_common_ali15x3 (ide_hwif_t *hwif)
  675. {
  676. hwif->autodma = 0;
  677. hwif->tuneproc = &ali15x3_tune_drive;
  678. hwif->speedproc = &ali15x3_tune_chipset;
  679. /* don't use LBA48 DMA on ALi devices before rev 0xC5 */
  680. hwif->no_lba48_dma = (m5229_revision <= 0xC4) ? 1 : 0;
  681. if (!hwif->dma_base) {
  682. hwif->drives[0].autotune = 1;
  683. hwif->drives[1].autotune = 1;
  684. return;
  685. }
  686. hwif->atapi_dma = 1;
  687. if (m5229_revision > 0x20)
  688. hwif->ultra_mask = 0x7f;
  689. hwif->mwdma_mask = 0x07;
  690. hwif->swdma_mask = 0x07;
  691. if (m5229_revision >= 0x20) {
  692. /*
  693. * M1543C or newer for DMAing
  694. */
  695. hwif->ide_dma_check = &ali15x3_config_drive_for_dma;
  696. hwif->dma_setup = &ali15x3_dma_setup;
  697. if (!noautodma)
  698. hwif->autodma = 1;
  699. if (!(hwif->udma_four))
  700. hwif->udma_four = ata66_ali15x3(hwif);
  701. }
  702. hwif->drives[0].autodma = hwif->autodma;
  703. hwif->drives[1].autodma = hwif->autodma;
  704. }
  705. /**
  706. * init_hwif_ali15x3 - Initialize the ALI IDE x86 stuff
  707. * @hwif: interface to configure
  708. *
  709. * Obtain the IRQ tables for an ALi based IDE solution on the PC
  710. * class platforms. This part of the code isn't applicable to the
  711. * Sparc systems
  712. */
  713. static void __devinit init_hwif_ali15x3 (ide_hwif_t *hwif)
  714. {
  715. u8 ideic, inmir;
  716. s8 irq_routing_table[] = { -1, 9, 3, 10, 4, 5, 7, 6,
  717. 1, 11, 0, 12, 0, 14, 0, 15 };
  718. int irq = -1;
  719. if (hwif->pci_dev->device == PCI_DEVICE_ID_AL_M5229)
  720. hwif->irq = hwif->channel ? 15 : 14;
  721. if (isa_dev) {
  722. /*
  723. * read IDE interface control
  724. */
  725. pci_read_config_byte(isa_dev, 0x58, &ideic);
  726. /* bit0, bit1 */
  727. ideic = ideic & 0x03;
  728. /* get IRQ for IDE Controller */
  729. if ((hwif->channel && ideic == 0x03) ||
  730. (!hwif->channel && !ideic)) {
  731. /*
  732. * get SIRQ1 routing table
  733. */
  734. pci_read_config_byte(isa_dev, 0x44, &inmir);
  735. inmir = inmir & 0x0f;
  736. irq = irq_routing_table[inmir];
  737. } else if (hwif->channel && !(ideic & 0x01)) {
  738. /*
  739. * get SIRQ2 routing table
  740. */
  741. pci_read_config_byte(isa_dev, 0x75, &inmir);
  742. inmir = inmir & 0x0f;
  743. irq = irq_routing_table[inmir];
  744. }
  745. if(irq >= 0)
  746. hwif->irq = irq;
  747. }
  748. init_hwif_common_ali15x3(hwif);
  749. }
  750. /**
  751. * init_dma_ali15x3 - set up DMA on ALi15x3
  752. * @hwif: IDE interface
  753. * @dmabase: DMA interface base PCI address
  754. *
  755. * Set up the DMA functionality on the ALi 15x3. For the ALi
  756. * controllers this is generic so we can let the generic code do
  757. * the actual work.
  758. */
  759. static void __devinit init_dma_ali15x3 (ide_hwif_t *hwif, unsigned long dmabase)
  760. {
  761. if (m5229_revision < 0x20)
  762. return;
  763. if (!hwif->channel)
  764. outb(inb(dmabase + 2) & 0x60, dmabase + 2);
  765. ide_setup_dma(hwif, dmabase, 8);
  766. }
  767. static ide_pci_device_t ali15x3_chipset __devinitdata = {
  768. .name = "ALI15X3",
  769. .init_chipset = init_chipset_ali15x3,
  770. .init_hwif = init_hwif_ali15x3,
  771. .init_dma = init_dma_ali15x3,
  772. .channels = 2,
  773. .autodma = AUTODMA,
  774. .bootable = ON_BOARD,
  775. };
  776. /**
  777. * alim15x3_init_one - set up an ALi15x3 IDE controller
  778. * @dev: PCI device to set up
  779. *
  780. * Perform the actual set up for an ALi15x3 that has been found by the
  781. * hot plug layer.
  782. */
  783. static int __devinit alim15x3_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  784. {
  785. static struct pci_device_id ati_rs100[] = {
  786. { PCI_DEVICE(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_RS100) },
  787. { },
  788. };
  789. ide_pci_device_t *d = &ali15x3_chipset;
  790. if (pci_dev_present(ati_rs100))
  791. printk(KERN_WARNING "alim15x3: ATI Radeon IGP Northbridge is not yet fully tested.\n");
  792. #if defined(CONFIG_SPARC64)
  793. d->init_hwif = init_hwif_common_ali15x3;
  794. #endif /* CONFIG_SPARC64 */
  795. return ide_setup_pci_device(dev, d);
  796. }
  797. static struct pci_device_id alim15x3_pci_tbl[] = {
  798. { PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5229, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  799. { PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M5228, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  800. { 0, },
  801. };
  802. MODULE_DEVICE_TABLE(pci, alim15x3_pci_tbl);
  803. static struct pci_driver driver = {
  804. .name = "ALI15x3_IDE",
  805. .id_table = alim15x3_pci_tbl,
  806. .probe = alim15x3_init_one,
  807. };
  808. static int __init ali15x3_ide_init(void)
  809. {
  810. return ide_pci_register_driver(&driver);
  811. }
  812. module_init(ali15x3_ide_init);
  813. MODULE_AUTHOR("Michael Aubry, Andrzej Krzysztofowicz, CJ, Andre Hedrick, Alan Cox");
  814. MODULE_DESCRIPTION("PCI driver module for ALi 15x3 IDE");
  815. MODULE_LICENSE("GPL");