at91_ide.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * IDE host driver for AT91 (SAM9, CAP9, AT572D940HF) Static Memory Controller
  3. * with Compact Flash True IDE logic
  4. *
  5. * Copyright (c) 2008, 2009 Kelvatek Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/version.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/clk.h>
  26. #include <linux/err.h>
  27. #include <linux/ide.h>
  28. #include <linux/platform_device.h>
  29. #include <mach/board.h>
  30. #include <mach/gpio.h>
  31. #include <mach/at91sam9263.h>
  32. #include <mach/at91sam9_smc.h>
  33. #include <mach/at91sam9263_matrix.h>
  34. #define DRV_NAME "at91_ide"
  35. #define perr(fmt, args...) pr_err(DRV_NAME ": " fmt, ##args)
  36. #define pdbg(fmt, args...) pr_debug("%s " fmt, __func__, ##args)
  37. /*
  38. * Access to IDE device is possible through EBI Static Memory Controller
  39. * with Compact Flash logic. For details see EBI and SMC datasheet sections
  40. * of any microcontroller from AT91SAM9 family.
  41. *
  42. * Within SMC chip select address space, lines A[23:21] distinguish Compact
  43. * Flash modes (I/O, common memory, attribute memory, True IDE). IDE modes are:
  44. * 0x00c0000 - True IDE
  45. * 0x00e0000 - Alternate True IDE (Alt Status Register)
  46. *
  47. * On True IDE mode Task File and Data Register are mapped at the same address.
  48. * To distinguish access between these two different bus data width is used:
  49. * 8Bit for Task File, 16Bit for Data I/O.
  50. *
  51. * After initialization we do 8/16 bit flipping (changes in SMC MODE register)
  52. * only inside IDE callback routines which are serialized by IDE layer,
  53. * so no additional locking needed.
  54. */
  55. #define TASK_FILE 0x00c00000
  56. #define ALT_MODE 0x00e00000
  57. #define REGS_SIZE 8
  58. #define enter_16bit(cs, mode) do { \
  59. mode = at91_sys_read(AT91_SMC_MODE(cs)); \
  60. at91_sys_write(AT91_SMC_MODE(cs), mode | AT91_SMC_DBW_16); \
  61. } while (0)
  62. #define leave_16bit(cs, mode) at91_sys_write(AT91_SMC_MODE(cs), mode);
  63. static void set_smc_timings(const u8 chipselect, const u16 cycle,
  64. const u16 setup, const u16 pulse,
  65. const u16 data_float, int use_iordy)
  66. {
  67. unsigned long mode = AT91_SMC_READMODE | AT91_SMC_WRITEMODE |
  68. AT91_SMC_BAT_SELECT;
  69. /* disable or enable waiting for IORDY signal */
  70. if (use_iordy)
  71. mode |= AT91_SMC_EXNWMODE_READY;
  72. /* add data float cycles if needed */
  73. if (data_float)
  74. mode |= AT91_SMC_TDF_(data_float);
  75. at91_sys_write(AT91_SMC_MODE(chipselect), mode);
  76. /* setup timings in SMC */
  77. at91_sys_write(AT91_SMC_SETUP(chipselect), AT91_SMC_NWESETUP_(setup) |
  78. AT91_SMC_NCS_WRSETUP_(0) |
  79. AT91_SMC_NRDSETUP_(setup) |
  80. AT91_SMC_NCS_RDSETUP_(0));
  81. at91_sys_write(AT91_SMC_PULSE(chipselect), AT91_SMC_NWEPULSE_(pulse) |
  82. AT91_SMC_NCS_WRPULSE_(cycle) |
  83. AT91_SMC_NRDPULSE_(pulse) |
  84. AT91_SMC_NCS_RDPULSE_(cycle));
  85. at91_sys_write(AT91_SMC_CYCLE(chipselect), AT91_SMC_NWECYCLE_(cycle) |
  86. AT91_SMC_NRDCYCLE_(cycle));
  87. }
  88. static unsigned int calc_mck_cycles(unsigned int ns, unsigned int mck_hz)
  89. {
  90. u64 tmp = ns;
  91. tmp *= mck_hz;
  92. tmp += 1000*1000*1000 - 1; /* round up */
  93. do_div(tmp, 1000*1000*1000);
  94. return (unsigned int) tmp;
  95. }
  96. static void apply_timings(const u8 chipselect, const u8 pio,
  97. const struct ide_timing *timing, int use_iordy)
  98. {
  99. unsigned int t0, t1, t2, t6z;
  100. unsigned int cycle, setup, pulse, data_float;
  101. unsigned int mck_hz;
  102. struct clk *mck;
  103. /* see table 22 of Compact Flash standard 4.1 for the meaning,
  104. * we do not stretch active (t2) time, so setup (t1) + hold time (th)
  105. * assure at least minimal recovery (t2i) time */
  106. t0 = timing->cyc8b;
  107. t1 = timing->setup;
  108. t2 = timing->act8b;
  109. t6z = (pio < 5) ? 30 : 20;
  110. pdbg("t0=%u t1=%u t2=%u t6z=%u\n", t0, t1, t2, t6z);
  111. mck = clk_get(NULL, "mck");
  112. BUG_ON(IS_ERR(mck));
  113. mck_hz = clk_get_rate(mck);
  114. pdbg("mck_hz=%u\n", mck_hz);
  115. cycle = calc_mck_cycles(t0, mck_hz);
  116. setup = calc_mck_cycles(t1, mck_hz);
  117. pulse = calc_mck_cycles(t2, mck_hz);
  118. data_float = calc_mck_cycles(t6z, mck_hz);
  119. pdbg("cycle=%u setup=%u pulse=%u data_float=%u\n",
  120. cycle, setup, pulse, data_float);
  121. set_smc_timings(chipselect, cycle, setup, pulse, data_float, use_iordy);
  122. }
  123. static void at91_ide_input_data(ide_drive_t *drive, struct request *rq,
  124. void *buf, unsigned int len)
  125. {
  126. ide_hwif_t *hwif = drive->hwif;
  127. struct ide_io_ports *io_ports = &hwif->io_ports;
  128. u8 chipselect = hwif->select_data;
  129. unsigned long mode;
  130. pdbg("cs %u buf %p len %d\n", chipselect, buf, len);
  131. len++;
  132. enter_16bit(chipselect, mode);
  133. __ide_mm_insw((void __iomem *) io_ports->data_addr, buf, len / 2);
  134. leave_16bit(chipselect, mode);
  135. }
  136. static void at91_ide_output_data(ide_drive_t *drive, struct request *rq,
  137. void *buf, unsigned int len)
  138. {
  139. ide_hwif_t *hwif = drive->hwif;
  140. struct ide_io_ports *io_ports = &hwif->io_ports;
  141. u8 chipselect = hwif->select_data;
  142. unsigned long mode;
  143. pdbg("cs %u buf %p len %d\n", chipselect, buf, len);
  144. enter_16bit(chipselect, mode);
  145. __ide_mm_outsw((void __iomem *) io_ports->data_addr, buf, len / 2);
  146. leave_16bit(chipselect, mode);
  147. }
  148. static u8 ide_mm_inb(unsigned long port)
  149. {
  150. return readb((void __iomem *) port);
  151. }
  152. static void ide_mm_outb(u8 value, unsigned long port)
  153. {
  154. writeb(value, (void __iomem *) port);
  155. }
  156. static void at91_ide_tf_load(ide_drive_t *drive, ide_task_t *task)
  157. {
  158. ide_hwif_t *hwif = drive->hwif;
  159. struct ide_io_ports *io_ports = &hwif->io_ports;
  160. struct ide_taskfile *tf = &task->tf;
  161. u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  162. if (task->tf_flags & IDE_TFLAG_FLAGGED)
  163. HIHI = 0xFF;
  164. if (task->tf_flags & IDE_TFLAG_OUT_DATA) {
  165. u16 data = (tf->hob_data << 8) | tf->data;
  166. at91_ide_output_data(drive, NULL, &data, 2);
  167. }
  168. if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  169. ide_mm_outb(tf->hob_feature, io_ports->feature_addr);
  170. if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  171. ide_mm_outb(tf->hob_nsect, io_ports->nsect_addr);
  172. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  173. ide_mm_outb(tf->hob_lbal, io_ports->lbal_addr);
  174. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  175. ide_mm_outb(tf->hob_lbam, io_ports->lbam_addr);
  176. if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  177. ide_mm_outb(tf->hob_lbah, io_ports->lbah_addr);
  178. if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  179. ide_mm_outb(tf->feature, io_ports->feature_addr);
  180. if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  181. ide_mm_outb(tf->nsect, io_ports->nsect_addr);
  182. if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  183. ide_mm_outb(tf->lbal, io_ports->lbal_addr);
  184. if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  185. ide_mm_outb(tf->lbam, io_ports->lbam_addr);
  186. if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  187. ide_mm_outb(tf->lbah, io_ports->lbah_addr);
  188. if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  189. ide_mm_outb((tf->device & HIHI) | drive->select, io_ports->device_addr);
  190. }
  191. static void at91_ide_tf_read(ide_drive_t *drive, ide_task_t *task)
  192. {
  193. ide_hwif_t *hwif = drive->hwif;
  194. struct ide_io_ports *io_ports = &hwif->io_ports;
  195. struct ide_taskfile *tf = &task->tf;
  196. if (task->tf_flags & IDE_TFLAG_IN_DATA) {
  197. u16 data;
  198. at91_ide_input_data(drive, NULL, &data, 2);
  199. tf->data = data & 0xff;
  200. tf->hob_data = (data >> 8) & 0xff;
  201. }
  202. /* be sure we're looking at the low order bits */
  203. ide_mm_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
  204. if (task->tf_flags & IDE_TFLAG_IN_FEATURE)
  205. tf->feature = ide_mm_inb(io_ports->feature_addr);
  206. if (task->tf_flags & IDE_TFLAG_IN_NSECT)
  207. tf->nsect = ide_mm_inb(io_ports->nsect_addr);
  208. if (task->tf_flags & IDE_TFLAG_IN_LBAL)
  209. tf->lbal = ide_mm_inb(io_ports->lbal_addr);
  210. if (task->tf_flags & IDE_TFLAG_IN_LBAM)
  211. tf->lbam = ide_mm_inb(io_ports->lbam_addr);
  212. if (task->tf_flags & IDE_TFLAG_IN_LBAH)
  213. tf->lbah = ide_mm_inb(io_ports->lbah_addr);
  214. if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
  215. tf->device = ide_mm_inb(io_ports->device_addr);
  216. if (task->tf_flags & IDE_TFLAG_LBA48) {
  217. ide_mm_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
  218. if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  219. tf->hob_feature = ide_mm_inb(io_ports->feature_addr);
  220. if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  221. tf->hob_nsect = ide_mm_inb(io_ports->nsect_addr);
  222. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  223. tf->hob_lbal = ide_mm_inb(io_ports->lbal_addr);
  224. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  225. tf->hob_lbam = ide_mm_inb(io_ports->lbam_addr);
  226. if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  227. tf->hob_lbah = ide_mm_inb(io_ports->lbah_addr);
  228. }
  229. }
  230. static void at91_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
  231. {
  232. struct ide_timing *timing;
  233. u8 chipselect = drive->hwif->select_data;
  234. int use_iordy = 0;
  235. pdbg("chipselect %u pio %u\n", chipselect, pio);
  236. timing = ide_timing_find_mode(XFER_PIO_0 + pio);
  237. BUG_ON(!timing);
  238. if ((pio > 2 || ata_id_has_iordy(drive->id)) &&
  239. !(ata_id_is_cfa(drive->id) && pio > 4))
  240. use_iordy = 1;
  241. apply_timings(chipselect, pio, timing, use_iordy);
  242. }
  243. static const struct ide_tp_ops at91_ide_tp_ops = {
  244. .exec_command = ide_exec_command,
  245. .read_status = ide_read_status,
  246. .read_altstatus = ide_read_altstatus,
  247. .set_irq = ide_set_irq,
  248. .tf_load = at91_ide_tf_load,
  249. .tf_read = at91_ide_tf_read,
  250. .input_data = at91_ide_input_data,
  251. .output_data = at91_ide_output_data,
  252. };
  253. static const struct ide_port_ops at91_ide_port_ops = {
  254. .set_pio_mode = at91_ide_set_pio_mode,
  255. };
  256. static const struct ide_port_info at91_ide_port_info __initdata = {
  257. .port_ops = &at91_ide_port_ops,
  258. .tp_ops = &at91_ide_tp_ops,
  259. .host_flags = IDE_HFLAG_MMIO | IDE_HFLAG_NO_DMA | IDE_HFLAG_SINGLE |
  260. IDE_HFLAG_NO_IO_32BIT | IDE_HFLAG_UNMASK_IRQS,
  261. .pio_mask = ATA_PIO5,
  262. };
  263. /*
  264. * If interrupt is delivered through GPIO, IRQ are triggered on falling
  265. * and rising edge of signal. Whereas IDE device request interrupt on high
  266. * level (rising edge in our case). This mean we have fake interrupts, so
  267. * we need to check interrupt pin and exit instantly from ISR when line
  268. * is on low level.
  269. */
  270. irqreturn_t at91_irq_handler(int irq, void *dev_id)
  271. {
  272. int ntries = 8;
  273. int pin_val1, pin_val2;
  274. /* additional deglitch, line can be noisy in badly designed PCB */
  275. do {
  276. pin_val1 = at91_get_gpio_value(irq);
  277. pin_val2 = at91_get_gpio_value(irq);
  278. } while (pin_val1 != pin_val2 && --ntries > 0);
  279. if (pin_val1 == 0 || ntries <= 0)
  280. return IRQ_HANDLED;
  281. return ide_intr(irq, dev_id);
  282. }
  283. static int __init at91_ide_probe(struct platform_device *pdev)
  284. {
  285. int ret;
  286. hw_regs_t hw;
  287. hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
  288. struct ide_host *host;
  289. struct resource *res;
  290. unsigned long tf_base = 0, ctl_base = 0;
  291. struct at91_cf_data *board = pdev->dev.platform_data;
  292. if (!board)
  293. return -ENODEV;
  294. if (board->det_pin && at91_get_gpio_value(board->det_pin) != 0) {
  295. perr("no device detected\n");
  296. return -ENODEV;
  297. }
  298. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  299. if (!res) {
  300. perr("can't get memory resource\n");
  301. return -ENODEV;
  302. }
  303. if (!devm_request_mem_region(&pdev->dev, res->start + TASK_FILE,
  304. REGS_SIZE, "ide") ||
  305. !devm_request_mem_region(&pdev->dev, res->start + ALT_MODE,
  306. REGS_SIZE, "alt")) {
  307. perr("memory resources in use\n");
  308. return -EBUSY;
  309. }
  310. pdbg("chipselect %u irq %u res %08lx\n", board->chipselect,
  311. board->irq_pin, (unsigned long) res->start);
  312. tf_base = (unsigned long) devm_ioremap(&pdev->dev, res->start + TASK_FILE,
  313. REGS_SIZE);
  314. ctl_base = (unsigned long) devm_ioremap(&pdev->dev, res->start + ALT_MODE,
  315. REGS_SIZE);
  316. if (!tf_base || !ctl_base) {
  317. perr("can't map memory regions\n");
  318. return -EBUSY;
  319. }
  320. memset(&hw, 0, sizeof(hw));
  321. if (board->flags & AT91_IDE_SWAP_A0_A2) {
  322. /* workaround for stupid hardware bug */
  323. hw.io_ports.data_addr = tf_base + 0;
  324. hw.io_ports.error_addr = tf_base + 4;
  325. hw.io_ports.nsect_addr = tf_base + 2;
  326. hw.io_ports.lbal_addr = tf_base + 6;
  327. hw.io_ports.lbam_addr = tf_base + 1;
  328. hw.io_ports.lbah_addr = tf_base + 5;
  329. hw.io_ports.device_addr = tf_base + 3;
  330. hw.io_ports.command_addr = tf_base + 7;
  331. hw.io_ports.ctl_addr = ctl_base + 3;
  332. } else
  333. ide_std_init_ports(&hw, tf_base, ctl_base + 6);
  334. hw.irq = board->irq_pin;
  335. hw.chipset = ide_generic;
  336. hw.dev = &pdev->dev;
  337. host = ide_host_alloc(&at91_ide_port_info, hws);
  338. if (!host) {
  339. perr("failed to allocate ide host\n");
  340. return -ENOMEM;
  341. }
  342. /* setup Static Memory Controller - PIO 0 as default */
  343. apply_timings(board->chipselect, 0, ide_timing_find_mode(XFER_PIO_0), 0);
  344. /* with GPIO interrupt we have to do quirks in handler */
  345. if (board->irq_pin >= PIN_BASE)
  346. host->irq_handler = at91_irq_handler;
  347. host->ports[0]->select_data = board->chipselect;
  348. ret = ide_host_register(host, &at91_ide_port_info, hws);
  349. if (ret) {
  350. perr("failed to register ide host\n");
  351. goto err_free_host;
  352. }
  353. platform_set_drvdata(pdev, host);
  354. return 0;
  355. err_free_host:
  356. ide_host_free(host);
  357. return ret;
  358. }
  359. static int __exit at91_ide_remove(struct platform_device *pdev)
  360. {
  361. struct ide_host *host = platform_get_drvdata(pdev);
  362. ide_host_remove(host);
  363. return 0;
  364. }
  365. static struct platform_driver at91_ide_driver = {
  366. .driver = {
  367. .name = DRV_NAME,
  368. .owner = THIS_MODULE,
  369. },
  370. .remove = __exit_p(at91_ide_remove),
  371. };
  372. static int __init at91_ide_init(void)
  373. {
  374. return platform_driver_probe(&at91_ide_driver, at91_ide_probe);
  375. }
  376. static void __exit at91_ide_exit(void)
  377. {
  378. platform_driver_unregister(&at91_ide_driver);
  379. }
  380. module_init(at91_ide_init);
  381. module_exit(at91_ide_exit);
  382. MODULE_LICENSE("GPL");
  383. MODULE_AUTHOR("Stanislaw Gruszka <stf_xl@wp.pl>");