scc_pata.c 24 KB

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