scc_pata.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * Support for IDE interfaces on Celleb platform
  3. *
  4. * (C) Copyright 2006 TOSHIBA CORPORATION
  5. *
  6. * This code is based on drivers/ide/pci/siimage.c:
  7. * Copyright (C) 2001-2002 Andre Hedrick <andre@linux-ide.org>
  8. * Copyright (C) 2003 Red Hat <alan@redhat.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/module.h>
  26. #include <linux/pci.h>
  27. #include <linux/delay.h>
  28. #include <linux/hdreg.h>
  29. #include <linux/ide.h>
  30. #include <linux/init.h>
  31. #define PCI_DEVICE_ID_TOSHIBA_SCC_ATA 0x01b4
  32. #define SCC_PATA_NAME "scc IDE"
  33. #define TDVHSEL_MASTER 0x00000001
  34. #define TDVHSEL_SLAVE 0x00000004
  35. #define MODE_JCUSFEN 0x00000080
  36. #define CCKCTRL_ATARESET 0x00040000
  37. #define CCKCTRL_BUFCNT 0x00020000
  38. #define CCKCTRL_CRST 0x00010000
  39. #define CCKCTRL_OCLKEN 0x00000100
  40. #define CCKCTRL_ATACLKOEN 0x00000002
  41. #define CCKCTRL_LCLKEN 0x00000001
  42. #define QCHCD_IOS_SS 0x00000001
  43. #define QCHSD_STPDIAG 0x00020000
  44. #define INTMASK_MSK 0xD1000012
  45. #define INTSTS_SERROR 0x80000000
  46. #define INTSTS_PRERR 0x40000000
  47. #define INTSTS_RERR 0x10000000
  48. #define INTSTS_ICERR 0x01000000
  49. #define INTSTS_BMSINT 0x00000010
  50. #define INTSTS_BMHE 0x00000008
  51. #define INTSTS_IOIRQS 0x00000004
  52. #define INTSTS_INTRQ 0x00000002
  53. #define INTSTS_ACTEINT 0x00000001
  54. #define ECMODE_VALUE 0x01
  55. static struct scc_ports {
  56. unsigned long ctl, dma;
  57. ide_hwif_t *hwif; /* for removing port from system */
  58. } scc_ports[MAX_HWIFS];
  59. /* PIO transfer mode table */
  60. /* JCHST */
  61. static unsigned long JCHSTtbl[2][7] = {
  62. {0x0E, 0x05, 0x02, 0x03, 0x02, 0x00, 0x00}, /* 100MHz */
  63. {0x13, 0x07, 0x04, 0x04, 0x03, 0x00, 0x00} /* 133MHz */
  64. };
  65. /* JCHHT */
  66. static unsigned long JCHHTtbl[2][7] = {
  67. {0x0E, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00}, /* 100MHz */
  68. {0x13, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00} /* 133MHz */
  69. };
  70. /* JCHCT */
  71. static unsigned long JCHCTtbl[2][7] = {
  72. {0x1D, 0x1D, 0x1C, 0x0B, 0x06, 0x00, 0x00}, /* 100MHz */
  73. {0x27, 0x26, 0x26, 0x0E, 0x09, 0x00, 0x00} /* 133MHz */
  74. };
  75. /* DMA transfer mode table */
  76. /* JCHDCTM/JCHDCTS */
  77. static unsigned long JCHDCTxtbl[2][7] = {
  78. {0x0A, 0x06, 0x04, 0x03, 0x01, 0x00, 0x00}, /* 100MHz */
  79. {0x0E, 0x09, 0x06, 0x04, 0x02, 0x01, 0x00} /* 133MHz */
  80. };
  81. /* JCSTWTM/JCSTWTS */
  82. static unsigned long JCSTWTxtbl[2][7] = {
  83. {0x06, 0x04, 0x03, 0x02, 0x02, 0x02, 0x00}, /* 100MHz */
  84. {0x09, 0x06, 0x04, 0x02, 0x02, 0x02, 0x02} /* 133MHz */
  85. };
  86. /* JCTSS */
  87. static unsigned long JCTSStbl[2][7] = {
  88. {0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x00}, /* 100MHz */
  89. {0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05} /* 133MHz */
  90. };
  91. /* JCENVT */
  92. static unsigned long JCENVTtbl[2][7] = {
  93. {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00}, /* 100MHz */
  94. {0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02} /* 133MHz */
  95. };
  96. /* JCACTSELS/JCACTSELM */
  97. static unsigned long JCACTSELtbl[2][7] = {
  98. {0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00}, /* 100MHz */
  99. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01} /* 133MHz */
  100. };
  101. static u8 scc_ide_inb(unsigned long port)
  102. {
  103. u32 data = in_be32((void*)port);
  104. return (u8)data;
  105. }
  106. static void scc_ide_insw(unsigned long port, void *addr, u32 count)
  107. {
  108. u16 *ptr = (u16 *)addr;
  109. while (count--) {
  110. *ptr++ = le16_to_cpu(in_be32((void*)port));
  111. }
  112. }
  113. static void scc_ide_insl(unsigned long port, void *addr, u32 count)
  114. {
  115. u16 *ptr = (u16 *)addr;
  116. while (count--) {
  117. *ptr++ = le16_to_cpu(in_be32((void*)port));
  118. *ptr++ = le16_to_cpu(in_be32((void*)port));
  119. }
  120. }
  121. static void scc_ide_outb(u8 addr, unsigned long port)
  122. {
  123. out_be32((void*)port, addr);
  124. }
  125. static void
  126. scc_ide_outbsync(ide_drive_t * drive, u8 addr, unsigned long port)
  127. {
  128. ide_hwif_t *hwif = HWIF(drive);
  129. out_be32((void*)port, addr);
  130. eieio();
  131. in_be32((void*)(hwif->dma_base + 0x01c));
  132. eieio();
  133. }
  134. static void
  135. scc_ide_outsw(unsigned long port, void *addr, u32 count)
  136. {
  137. u16 *ptr = (u16 *)addr;
  138. while (count--) {
  139. out_be32((void*)port, cpu_to_le16(*ptr++));
  140. }
  141. }
  142. static void
  143. scc_ide_outsl(unsigned long port, void *addr, u32 count)
  144. {
  145. u16 *ptr = (u16 *)addr;
  146. while (count--) {
  147. out_be32((void*)port, cpu_to_le16(*ptr++));
  148. out_be32((void*)port, cpu_to_le16(*ptr++));
  149. }
  150. }
  151. /**
  152. * scc_set_pio_mode - set host controller for PIO mode
  153. * @drive: drive
  154. * @pio: PIO mode number
  155. *
  156. * Load the timing settings for this device mode into the
  157. * controller.
  158. */
  159. static void scc_set_pio_mode(ide_drive_t *drive, const u8 pio)
  160. {
  161. ide_hwif_t *hwif = HWIF(drive);
  162. struct scc_ports *ports = ide_get_hwifdata(hwif);
  163. unsigned long ctl_base = ports->ctl;
  164. unsigned long cckctrl_port = ctl_base + 0xff0;
  165. unsigned long piosht_port = ctl_base + 0x000;
  166. unsigned long pioct_port = ctl_base + 0x004;
  167. unsigned long reg;
  168. int offset;
  169. reg = in_be32((void __iomem *)cckctrl_port);
  170. if (reg & CCKCTRL_ATACLKOEN) {
  171. offset = 1; /* 133MHz */
  172. } else {
  173. offset = 0; /* 100MHz */
  174. }
  175. reg = JCHSTtbl[offset][pio] << 16 | JCHHTtbl[offset][pio];
  176. out_be32((void __iomem *)piosht_port, reg);
  177. reg = JCHCTtbl[offset][pio];
  178. out_be32((void __iomem *)pioct_port, reg);
  179. }
  180. /**
  181. * scc_set_dma_mode - set host controller for DMA mode
  182. * @drive: drive
  183. * @speed: DMA mode
  184. *
  185. * Load the timing settings for this device mode into the
  186. * controller.
  187. */
  188. static void scc_set_dma_mode(ide_drive_t *drive, const u8 speed)
  189. {
  190. ide_hwif_t *hwif = HWIF(drive);
  191. struct scc_ports *ports = ide_get_hwifdata(hwif);
  192. unsigned long ctl_base = ports->ctl;
  193. unsigned long cckctrl_port = ctl_base + 0xff0;
  194. unsigned long mdmact_port = ctl_base + 0x008;
  195. unsigned long mcrcst_port = ctl_base + 0x00c;
  196. unsigned long sdmact_port = ctl_base + 0x010;
  197. unsigned long scrcst_port = ctl_base + 0x014;
  198. unsigned long udenvt_port = ctl_base + 0x018;
  199. unsigned long tdvhsel_port = ctl_base + 0x020;
  200. int is_slave = (&hwif->drives[1] == drive);
  201. int offset, idx;
  202. unsigned long reg;
  203. unsigned long jcactsel;
  204. reg = in_be32((void __iomem *)cckctrl_port);
  205. if (reg & CCKCTRL_ATACLKOEN) {
  206. offset = 1; /* 133MHz */
  207. } else {
  208. offset = 0; /* 100MHz */
  209. }
  210. idx = speed - XFER_UDMA_0;
  211. jcactsel = JCACTSELtbl[offset][idx];
  212. if (is_slave) {
  213. out_be32((void __iomem *)sdmact_port, JCHDCTxtbl[offset][idx]);
  214. out_be32((void __iomem *)scrcst_port, JCSTWTxtbl[offset][idx]);
  215. jcactsel = jcactsel << 2;
  216. out_be32((void __iomem *)tdvhsel_port, (in_be32((void __iomem *)tdvhsel_port) & ~TDVHSEL_SLAVE) | jcactsel);
  217. } else {
  218. out_be32((void __iomem *)mdmact_port, JCHDCTxtbl[offset][idx]);
  219. out_be32((void __iomem *)mcrcst_port, JCSTWTxtbl[offset][idx]);
  220. out_be32((void __iomem *)tdvhsel_port, (in_be32((void __iomem *)tdvhsel_port) & ~TDVHSEL_MASTER) | jcactsel);
  221. }
  222. reg = JCTSStbl[offset][idx] << 16 | JCENVTtbl[offset][idx];
  223. out_be32((void __iomem *)udenvt_port, reg);
  224. }
  225. static void scc_dma_host_set(ide_drive_t *drive, int on)
  226. {
  227. ide_hwif_t *hwif = drive->hwif;
  228. u8 unit = (drive->select.b.unit & 0x01);
  229. u8 dma_stat = scc_ide_inb(hwif->dma_status);
  230. if (on)
  231. dma_stat |= (1 << (5 + unit));
  232. else
  233. dma_stat &= ~(1 << (5 + unit));
  234. scc_ide_outb(dma_stat, hwif->dma_status);
  235. }
  236. /**
  237. * scc_ide_dma_setup - begin a DMA phase
  238. * @drive: target device
  239. *
  240. * Build an IDE DMA PRD (IDE speak for scatter gather table)
  241. * and then set up the DMA transfer registers.
  242. *
  243. * Returns 0 on success. If a PIO fallback is required then 1
  244. * is returned.
  245. */
  246. static int scc_dma_setup(ide_drive_t *drive)
  247. {
  248. ide_hwif_t *hwif = drive->hwif;
  249. struct request *rq = HWGROUP(drive)->rq;
  250. unsigned int reading;
  251. u8 dma_stat;
  252. if (rq_data_dir(rq))
  253. reading = 0;
  254. else
  255. reading = 1 << 3;
  256. /* fall back to pio! */
  257. if (!ide_build_dmatable(drive, rq)) {
  258. ide_map_sg(drive, rq);
  259. return 1;
  260. }
  261. /* PRD table */
  262. out_be32((void __iomem *)(hwif->dma_base + 8), hwif->dmatable_dma);
  263. /* specify r/w */
  264. out_be32((void __iomem *)hwif->dma_command, reading);
  265. /* read dma_status for INTR & ERROR flags */
  266. dma_stat = in_be32((void __iomem *)hwif->dma_status);
  267. /* clear INTR & ERROR flags */
  268. out_be32((void __iomem *)hwif->dma_status, dma_stat|6);
  269. drive->waiting_for_dma = 1;
  270. return 0;
  271. }
  272. static void scc_dma_start(ide_drive_t *drive)
  273. {
  274. ide_hwif_t *hwif = drive->hwif;
  275. u8 dma_cmd = scc_ide_inb(hwif->dma_command);
  276. /* start DMA */
  277. scc_ide_outb(dma_cmd | 1, hwif->dma_command);
  278. hwif->dma = 1;
  279. wmb();
  280. }
  281. static int __scc_dma_end(ide_drive_t *drive)
  282. {
  283. ide_hwif_t *hwif = drive->hwif;
  284. u8 dma_stat, dma_cmd;
  285. drive->waiting_for_dma = 0;
  286. /* get DMA command mode */
  287. dma_cmd = scc_ide_inb(hwif->dma_command);
  288. /* stop DMA */
  289. scc_ide_outb(dma_cmd & ~1, hwif->dma_command);
  290. /* get DMA status */
  291. dma_stat = scc_ide_inb(hwif->dma_status);
  292. /* clear the INTR & ERROR bits */
  293. scc_ide_outb(dma_stat | 6, hwif->dma_status);
  294. /* purge DMA mappings */
  295. ide_destroy_dmatable(drive);
  296. /* verify good DMA status */
  297. hwif->dma = 0;
  298. wmb();
  299. return (dma_stat & 7) != 4 ? (0x10 | dma_stat) : 0;
  300. }
  301. /**
  302. * scc_dma_end - Stop DMA
  303. * @drive: IDE drive
  304. *
  305. * Check and clear INT Status register.
  306. * Then call __scc_dma_end().
  307. */
  308. static int scc_dma_end(ide_drive_t *drive)
  309. {
  310. ide_hwif_t *hwif = HWIF(drive);
  311. unsigned long intsts_port = hwif->dma_base + 0x014;
  312. u32 reg;
  313. int dma_stat, data_loss = 0;
  314. static int retry = 0;
  315. /* errata A308 workaround: Step5 (check data loss) */
  316. /* We don't check non ide_disk because it is limited to UDMA4 */
  317. if (!(in_be32((void __iomem *)hwif->io_ports.ctl_addr)
  318. & ERR_STAT) &&
  319. drive->media == ide_disk && drive->current_speed > XFER_UDMA_4) {
  320. reg = in_be32((void __iomem *)intsts_port);
  321. if (!(reg & INTSTS_ACTEINT)) {
  322. printk(KERN_WARNING "%s: operation failed (transfer data loss)\n",
  323. drive->name);
  324. data_loss = 1;
  325. if (retry++) {
  326. struct request *rq = HWGROUP(drive)->rq;
  327. int unit;
  328. /* ERROR_RESET and drive->crc_count are needed
  329. * to reduce DMA transfer mode in retry process.
  330. */
  331. if (rq)
  332. rq->errors |= ERROR_RESET;
  333. for (unit = 0; unit < MAX_DRIVES; unit++) {
  334. ide_drive_t *drive = &hwif->drives[unit];
  335. drive->crc_count++;
  336. }
  337. }
  338. }
  339. }
  340. while (1) {
  341. reg = in_be32((void __iomem *)intsts_port);
  342. if (reg & INTSTS_SERROR) {
  343. printk(KERN_WARNING "%s: SERROR\n", SCC_PATA_NAME);
  344. out_be32((void __iomem *)intsts_port, INTSTS_SERROR|INTSTS_BMSINT);
  345. out_be32((void __iomem *)hwif->dma_command, in_be32((void __iomem *)hwif->dma_command) & ~QCHCD_IOS_SS);
  346. continue;
  347. }
  348. if (reg & INTSTS_PRERR) {
  349. u32 maea0, maec0;
  350. unsigned long ctl_base = hwif->config_data;
  351. maea0 = in_be32((void __iomem *)(ctl_base + 0xF50));
  352. maec0 = in_be32((void __iomem *)(ctl_base + 0xF54));
  353. printk(KERN_WARNING "%s: PRERR [addr:%x cmd:%x]\n", SCC_PATA_NAME, maea0, maec0);
  354. out_be32((void __iomem *)intsts_port, INTSTS_PRERR|INTSTS_BMSINT);
  355. out_be32((void __iomem *)hwif->dma_command, in_be32((void __iomem *)hwif->dma_command) & ~QCHCD_IOS_SS);
  356. continue;
  357. }
  358. if (reg & INTSTS_RERR) {
  359. printk(KERN_WARNING "%s: Response Error\n", SCC_PATA_NAME);
  360. out_be32((void __iomem *)intsts_port, INTSTS_RERR|INTSTS_BMSINT);
  361. out_be32((void __iomem *)hwif->dma_command, in_be32((void __iomem *)hwif->dma_command) & ~QCHCD_IOS_SS);
  362. continue;
  363. }
  364. if (reg & INTSTS_ICERR) {
  365. out_be32((void __iomem *)hwif->dma_command, in_be32((void __iomem *)hwif->dma_command) & ~QCHCD_IOS_SS);
  366. printk(KERN_WARNING "%s: Illegal Configuration\n", SCC_PATA_NAME);
  367. out_be32((void __iomem *)intsts_port, INTSTS_ICERR|INTSTS_BMSINT);
  368. continue;
  369. }
  370. if (reg & INTSTS_BMSINT) {
  371. printk(KERN_WARNING "%s: Internal Bus Error\n", SCC_PATA_NAME);
  372. out_be32((void __iomem *)intsts_port, INTSTS_BMSINT);
  373. ide_do_reset(drive);
  374. continue;
  375. }
  376. if (reg & INTSTS_BMHE) {
  377. out_be32((void __iomem *)intsts_port, INTSTS_BMHE);
  378. continue;
  379. }
  380. if (reg & INTSTS_ACTEINT) {
  381. out_be32((void __iomem *)intsts_port, INTSTS_ACTEINT);
  382. continue;
  383. }
  384. if (reg & INTSTS_IOIRQS) {
  385. out_be32((void __iomem *)intsts_port, INTSTS_IOIRQS);
  386. continue;
  387. }
  388. break;
  389. }
  390. dma_stat = __scc_dma_end(drive);
  391. if (data_loss)
  392. dma_stat |= 2; /* emulate DMA error (to retry command) */
  393. return dma_stat;
  394. }
  395. /* returns 1 if dma irq issued, 0 otherwise */
  396. static int scc_dma_test_irq(ide_drive_t *drive)
  397. {
  398. ide_hwif_t *hwif = HWIF(drive);
  399. u32 int_stat = in_be32((void __iomem *)hwif->dma_base + 0x014);
  400. /* SCC errata A252,A308 workaround: Step4 */
  401. if ((in_be32((void __iomem *)hwif->io_ports.ctl_addr)
  402. & ERR_STAT) &&
  403. (int_stat & INTSTS_INTRQ))
  404. return 1;
  405. /* SCC errata A308 workaround: Step5 (polling IOIRQS) */
  406. if (int_stat & INTSTS_IOIRQS)
  407. return 1;
  408. if (!drive->waiting_for_dma)
  409. printk(KERN_WARNING "%s: (%s) called while not waiting\n",
  410. drive->name, __func__);
  411. return 0;
  412. }
  413. static u8 scc_udma_filter(ide_drive_t *drive)
  414. {
  415. ide_hwif_t *hwif = drive->hwif;
  416. u8 mask = hwif->ultra_mask;
  417. /* errata A308 workaround: limit non ide_disk drive to UDMA4 */
  418. if ((drive->media != ide_disk) && (mask & 0xE0)) {
  419. printk(KERN_INFO "%s: limit %s to UDMA4\n",
  420. SCC_PATA_NAME, drive->name);
  421. mask = ATA_UDMA4;
  422. }
  423. return mask;
  424. }
  425. /**
  426. * setup_mmio_scc - map CTRL/BMID region
  427. * @dev: PCI device we are configuring
  428. * @name: device name
  429. *
  430. */
  431. static int setup_mmio_scc (struct pci_dev *dev, const char *name)
  432. {
  433. unsigned long ctl_base = pci_resource_start(dev, 0);
  434. unsigned long dma_base = pci_resource_start(dev, 1);
  435. unsigned long ctl_size = pci_resource_len(dev, 0);
  436. unsigned long dma_size = pci_resource_len(dev, 1);
  437. void __iomem *ctl_addr;
  438. void __iomem *dma_addr;
  439. int i, ret;
  440. for (i = 0; i < MAX_HWIFS; i++) {
  441. if (scc_ports[i].ctl == 0)
  442. break;
  443. }
  444. if (i >= MAX_HWIFS)
  445. return -ENOMEM;
  446. ret = pci_request_selected_regions(dev, (1 << 2) - 1, name);
  447. if (ret < 0) {
  448. printk(KERN_ERR "%s: can't reserve resources\n", name);
  449. return ret;
  450. }
  451. if ((ctl_addr = ioremap(ctl_base, ctl_size)) == NULL)
  452. goto fail_0;
  453. if ((dma_addr = ioremap(dma_base, dma_size)) == NULL)
  454. goto fail_1;
  455. pci_set_master(dev);
  456. scc_ports[i].ctl = (unsigned long)ctl_addr;
  457. scc_ports[i].dma = (unsigned long)dma_addr;
  458. pci_set_drvdata(dev, (void *) &scc_ports[i]);
  459. return 1;
  460. fail_1:
  461. iounmap(ctl_addr);
  462. fail_0:
  463. return -ENOMEM;
  464. }
  465. static int scc_ide_setup_pci_device(struct pci_dev *dev,
  466. const struct ide_port_info *d)
  467. {
  468. struct scc_ports *ports = pci_get_drvdata(dev);
  469. ide_hwif_t *hwif = NULL;
  470. hw_regs_t hw;
  471. u8 idx[4] = { 0xff, 0xff, 0xff, 0xff };
  472. int i;
  473. hwif = ide_find_port();
  474. if (hwif == NULL) {
  475. printk(KERN_ERR "%s: too many IDE interfaces, "
  476. "no room in table\n", SCC_PATA_NAME);
  477. return -ENOMEM;
  478. }
  479. memset(&hw, 0, sizeof(hw));
  480. for (i = 0; i <= 8; i++)
  481. hw.io_ports_array[i] = ports->dma + 0x20 + i * 4;
  482. hw.irq = dev->irq;
  483. hw.dev = &dev->dev;
  484. hw.chipset = ide_pci;
  485. ide_init_port_hw(hwif, &hw);
  486. hwif->dev = &dev->dev;
  487. idx[0] = hwif->index;
  488. ide_device_add(idx, d);
  489. return 0;
  490. }
  491. /**
  492. * init_setup_scc - set up an SCC PATA Controller
  493. * @dev: PCI device
  494. * @d: IDE port info
  495. *
  496. * Perform the initial set up for this device.
  497. */
  498. static int __devinit init_setup_scc(struct pci_dev *dev,
  499. const struct ide_port_info *d)
  500. {
  501. unsigned long ctl_base;
  502. unsigned long dma_base;
  503. unsigned long cckctrl_port;
  504. unsigned long intmask_port;
  505. unsigned long mode_port;
  506. unsigned long ecmode_port;
  507. unsigned long dma_status_port;
  508. u32 reg = 0;
  509. struct scc_ports *ports;
  510. int rc;
  511. rc = pci_enable_device(dev);
  512. if (rc)
  513. goto end;
  514. rc = setup_mmio_scc(dev, d->name);
  515. if (rc < 0)
  516. goto end;
  517. ports = pci_get_drvdata(dev);
  518. ctl_base = ports->ctl;
  519. dma_base = ports->dma;
  520. cckctrl_port = ctl_base + 0xff0;
  521. intmask_port = dma_base + 0x010;
  522. mode_port = ctl_base + 0x024;
  523. ecmode_port = ctl_base + 0xf00;
  524. dma_status_port = dma_base + 0x004;
  525. /* controller initialization */
  526. reg = 0;
  527. out_be32((void*)cckctrl_port, reg);
  528. reg |= CCKCTRL_ATACLKOEN;
  529. out_be32((void*)cckctrl_port, reg);
  530. reg |= CCKCTRL_LCLKEN | CCKCTRL_OCLKEN;
  531. out_be32((void*)cckctrl_port, reg);
  532. reg |= CCKCTRL_CRST;
  533. out_be32((void*)cckctrl_port, reg);
  534. for (;;) {
  535. reg = in_be32((void*)cckctrl_port);
  536. if (reg & CCKCTRL_CRST)
  537. break;
  538. udelay(5000);
  539. }
  540. reg |= CCKCTRL_ATARESET;
  541. out_be32((void*)cckctrl_port, reg);
  542. out_be32((void*)ecmode_port, ECMODE_VALUE);
  543. out_be32((void*)mode_port, MODE_JCUSFEN);
  544. out_be32((void*)intmask_port, INTMASK_MSK);
  545. rc = scc_ide_setup_pci_device(dev, d);
  546. end:
  547. return rc;
  548. }
  549. static void scc_tf_load(ide_drive_t *drive, ide_task_t *task)
  550. {
  551. struct ide_io_ports *io_ports = &drive->hwif->io_ports;
  552. struct ide_taskfile *tf = &task->tf;
  553. u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  554. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  555. HIHI = 0xFF;
  556. ide_set_irq(drive, 1);
  557. if (task->tf_flags & IDE_TFLAG_OUT_DATA)
  558. out_be32((void *)io_ports->data_addr,
  559. (tf->hob_data << 8) | tf->data);
  560. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  561. scc_ide_outb(tf->hob_feature, io_ports->feature_addr);
  562. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  563. scc_ide_outb(tf->hob_nsect, io_ports->nsect_addr);
  564. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  565. scc_ide_outb(tf->hob_lbal, io_ports->lbal_addr);
  566. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  567. scc_ide_outb(tf->hob_lbam, io_ports->lbam_addr);
  568. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  569. scc_ide_outb(tf->hob_lbah, io_ports->lbah_addr);
  570. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  571. scc_ide_outb(tf->feature, io_ports->feature_addr);
  572. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  573. scc_ide_outb(tf->nsect, io_ports->nsect_addr);
  574. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  575. scc_ide_outb(tf->lbal, io_ports->lbal_addr);
  576. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  577. scc_ide_outb(tf->lbam, io_ports->lbam_addr);
  578. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  579. scc_ide_outb(tf->lbah, io_ports->lbah_addr);
  580. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  581. scc_ide_outb((tf->device & HIHI) | drive->select.all,
  582. io_ports->device_addr);
  583. }
  584. static void scc_tf_read(ide_drive_t *drive, ide_task_t *task)
  585. {
  586. struct ide_io_ports *io_ports = &drive->hwif->io_ports;
  587. struct ide_taskfile *tf = &task->tf;
  588. if (task->tf_flags & IDE_TFLAG_IN_DATA) {
  589. u16 data = (u16)in_be32((void *)io_ports->data_addr);
  590. tf->data = data & 0xff;
  591. tf->hob_data = (data >> 8) & 0xff;
  592. }
  593. /* be sure we're looking at the low order bits */
  594. scc_ide_outb(drive->ctl & ~0x80, io_ports->ctl_addr);
  595. if (task->tf_flags & IDE_TFLAG_IN_NSECT)
  596. tf->nsect = scc_ide_inb(io_ports->nsect_addr);
  597. if (task->tf_flags & IDE_TFLAG_IN_LBAL)
  598. tf->lbal = scc_ide_inb(io_ports->lbal_addr);
  599. if (task->tf_flags & IDE_TFLAG_IN_LBAM)
  600. tf->lbam = scc_ide_inb(io_ports->lbam_addr);
  601. if (task->tf_flags & IDE_TFLAG_IN_LBAH)
  602. tf->lbah = scc_ide_inb(io_ports->lbah_addr);
  603. if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
  604. tf->device = scc_ide_inb(io_ports->device_addr);
  605. if (task->tf_flags & IDE_TFLAG_LBA48) {
  606. scc_ide_outb(drive->ctl | 0x80, io_ports->ctl_addr);
  607. if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  608. tf->hob_feature = scc_ide_inb(io_ports->feature_addr);
  609. if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  610. tf->hob_nsect = scc_ide_inb(io_ports->nsect_addr);
  611. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  612. tf->hob_lbal = scc_ide_inb(io_ports->lbal_addr);
  613. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  614. tf->hob_lbam = scc_ide_inb(io_ports->lbam_addr);
  615. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  616. tf->hob_lbah = scc_ide_inb(io_ports->lbah_addr);
  617. }
  618. }
  619. static void scc_input_data(ide_drive_t *drive, struct request *rq,
  620. void *buf, unsigned int len)
  621. {
  622. unsigned long data_addr = drive->hwif->io_ports.data_addr;
  623. len++;
  624. if (drive->io_32bit) {
  625. scc_ide_insl(data_addr, buf, len / 4);
  626. if ((len & 3) >= 2)
  627. scc_ide_insw(data_addr, (u8 *)buf + (len & ~3), 1);
  628. } else
  629. scc_ide_insw(data_addr, buf, len / 2);
  630. }
  631. static void scc_output_data(ide_drive_t *drive, struct request *rq,
  632. void *buf, unsigned int len)
  633. {
  634. unsigned long data_addr = drive->hwif->io_ports.data_addr;
  635. len++;
  636. if (drive->io_32bit) {
  637. scc_ide_outsl(data_addr, buf, len / 4);
  638. if ((len & 3) >= 2)
  639. scc_ide_outsw(data_addr, (u8 *)buf + (len & ~3), 1);
  640. } else
  641. scc_ide_outsw(data_addr, buf, len / 2);
  642. }
  643. /**
  644. * init_mmio_iops_scc - set up the iops for MMIO
  645. * @hwif: interface to set up
  646. *
  647. */
  648. static void __devinit init_mmio_iops_scc(ide_hwif_t *hwif)
  649. {
  650. struct pci_dev *dev = to_pci_dev(hwif->dev);
  651. struct scc_ports *ports = pci_get_drvdata(dev);
  652. unsigned long dma_base = ports->dma;
  653. ide_set_hwifdata(hwif, ports);
  654. hwif->tf_load = scc_tf_load;
  655. hwif->tf_read = scc_tf_read;
  656. hwif->input_data = scc_input_data;
  657. hwif->output_data = scc_output_data;
  658. hwif->INB = scc_ide_inb;
  659. hwif->OUTB = scc_ide_outb;
  660. hwif->OUTBSYNC = scc_ide_outbsync;
  661. hwif->dma_base = dma_base;
  662. hwif->config_data = ports->ctl;
  663. hwif->mmio = 1;
  664. }
  665. /**
  666. * init_iops_scc - set up iops
  667. * @hwif: interface to set up
  668. *
  669. * Do the basic setup for the SCC hardware interface
  670. * and then do the MMIO setup.
  671. */
  672. static void __devinit init_iops_scc(ide_hwif_t *hwif)
  673. {
  674. struct pci_dev *dev = to_pci_dev(hwif->dev);
  675. hwif->hwif_data = NULL;
  676. if (pci_get_drvdata(dev) == NULL)
  677. return;
  678. init_mmio_iops_scc(hwif);
  679. }
  680. static u8 __devinit scc_cable_detect(ide_hwif_t *hwif)
  681. {
  682. return ATA_CBL_PATA80;
  683. }
  684. /**
  685. * init_hwif_scc - set up hwif
  686. * @hwif: interface to set up
  687. *
  688. * We do the basic set up of the interface structure. The SCC
  689. * requires several custom handlers so we override the default
  690. * ide DMA handlers appropriately.
  691. */
  692. static void __devinit init_hwif_scc(ide_hwif_t *hwif)
  693. {
  694. struct scc_ports *ports = ide_get_hwifdata(hwif);
  695. ports->hwif = hwif;
  696. hwif->dma_command = hwif->dma_base;
  697. hwif->dma_status = hwif->dma_base + 0x04;
  698. /* PTERADD */
  699. out_be32((void __iomem *)(hwif->dma_base + 0x018), hwif->dmatable_dma);
  700. if (in_be32((void __iomem *)(hwif->config_data + 0xff0)) & CCKCTRL_ATACLKOEN)
  701. hwif->ultra_mask = ATA_UDMA6; /* 133MHz */
  702. else
  703. hwif->ultra_mask = ATA_UDMA5; /* 100MHz */
  704. }
  705. static const struct ide_port_ops scc_port_ops = {
  706. .set_pio_mode = scc_set_pio_mode,
  707. .set_dma_mode = scc_set_dma_mode,
  708. .udma_filter = scc_udma_filter,
  709. .cable_detect = scc_cable_detect,
  710. };
  711. static const struct ide_dma_ops scc_dma_ops = {
  712. .dma_host_set = scc_dma_host_set,
  713. .dma_setup = scc_dma_setup,
  714. .dma_exec_cmd = ide_dma_exec_cmd,
  715. .dma_start = scc_dma_start,
  716. .dma_end = scc_dma_end,
  717. .dma_test_irq = scc_dma_test_irq,
  718. .dma_lost_irq = ide_dma_lost_irq,
  719. .dma_timeout = ide_dma_timeout,
  720. };
  721. #define DECLARE_SCC_DEV(name_str) \
  722. { \
  723. .name = name_str, \
  724. .init_iops = init_iops_scc, \
  725. .init_hwif = init_hwif_scc, \
  726. .port_ops = &scc_port_ops, \
  727. .dma_ops = &scc_dma_ops, \
  728. .host_flags = IDE_HFLAG_SINGLE, \
  729. .pio_mask = ATA_PIO4, \
  730. }
  731. static const struct ide_port_info scc_chipsets[] __devinitdata = {
  732. /* 0 */ DECLARE_SCC_DEV("sccIDE"),
  733. };
  734. /**
  735. * scc_init_one - pci layer discovery entry
  736. * @dev: PCI device
  737. * @id: ident table entry
  738. *
  739. * Called by the PCI code when it finds an SCC PATA controller.
  740. * We then use the IDE PCI generic helper to do most of the work.
  741. */
  742. static int __devinit scc_init_one(struct pci_dev *dev, const struct pci_device_id *id)
  743. {
  744. return init_setup_scc(dev, &scc_chipsets[id->driver_data]);
  745. }
  746. /**
  747. * scc_remove - pci layer remove entry
  748. * @dev: PCI device
  749. *
  750. * Called by the PCI code when it removes an SCC PATA controller.
  751. */
  752. static void __devexit scc_remove(struct pci_dev *dev)
  753. {
  754. struct scc_ports *ports = pci_get_drvdata(dev);
  755. ide_hwif_t *hwif = ports->hwif;
  756. if (hwif->dmatable_cpu) {
  757. pci_free_consistent(dev, PRD_ENTRIES * PRD_BYTES,
  758. hwif->dmatable_cpu, hwif->dmatable_dma);
  759. hwif->dmatable_cpu = NULL;
  760. }
  761. ide_unregister(hwif);
  762. iounmap((void*)ports->dma);
  763. iounmap((void*)ports->ctl);
  764. pci_release_selected_regions(dev, (1 << 2) - 1);
  765. memset(ports, 0, sizeof(*ports));
  766. }
  767. static const struct pci_device_id scc_pci_tbl[] = {
  768. { PCI_VDEVICE(TOSHIBA_2, PCI_DEVICE_ID_TOSHIBA_SCC_ATA), 0 },
  769. { 0, },
  770. };
  771. MODULE_DEVICE_TABLE(pci, scc_pci_tbl);
  772. static struct pci_driver driver = {
  773. .name = "SCC IDE",
  774. .id_table = scc_pci_tbl,
  775. .probe = scc_init_one,
  776. .remove = scc_remove,
  777. };
  778. static int scc_ide_init(void)
  779. {
  780. return ide_pci_register_driver(&driver);
  781. }
  782. module_init(scc_ide_init);
  783. /* -- No exit code?
  784. static void scc_ide_exit(void)
  785. {
  786. ide_pci_unregister_driver(&driver);
  787. }
  788. module_exit(scc_ide_exit);
  789. */
  790. MODULE_DESCRIPTION("PCI driver module for Toshiba SCC IDE");
  791. MODULE_LICENSE("GPL");