100-magicbox-ide-driver.patch 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. --- a/drivers/ide/Kconfig
  2. +++ b/drivers/ide/Kconfig
  3. @@ -712,6 +712,11 @@ config BLK_DEV_IDE_AU1XXX_SEQTS_PER_RQ
  4. default "128"
  5. depends on BLK_DEV_IDE_AU1XXX
  6. +config BLK_DEV_IDE_MAGICBOX
  7. + tristate "Magicbox CF card support"
  8. + depends on MAGICBOXV2 || OPENRB_LIGHT
  9. + select IDE_XFER_MODE
  10. +
  11. config BLK_DEV_IDE_TX4938
  12. tristate "TX4938 internal IDE support"
  13. depends on SOC_TX4938
  14. --- a/drivers/ide/Makefile
  15. +++ b/drivers/ide/Makefile
  16. @@ -110,6 +110,7 @@ obj-$(CONFIG_BLK_DEV_IDE_RAPIDE) += rapi
  17. obj-$(CONFIG_BLK_DEV_PALMCHIP_BK3710) += palm_bk3710.o
  18. obj-$(CONFIG_BLK_DEV_IDE_AU1XXX) += au1xxx-ide.o
  19. +obj-$(CONFIG_BLK_DEV_IDE_MAGICBOX) += magicbox_ide.o
  20. obj-$(CONFIG_BLK_DEV_IDE_TX4938) += tx4938ide.o
  21. obj-$(CONFIG_BLK_DEV_IDE_TX4939) += tx4939ide.o
  22. --- /dev/null
  23. +++ b/drivers/ide/magicbox_ide.c
  24. @@ -0,0 +1,332 @@
  25. +/*
  26. + * IDE driver for the MagicBox 2.0 onboard CompactFlash slot.
  27. + *
  28. + * Copyright (C) 2009 Gabor Juhos <[email protected]>
  29. + *
  30. + * Based on the original driver by Wojtek Kaniewski <[email protected]>
  31. + *
  32. + * This program is free software; you can redistribute it and/or modify it
  33. + * under the terms of the GNU General Public License version 2 as published
  34. + * by the Free Software Foundation.
  35. + */
  36. +
  37. +#include <linux/types.h>
  38. +#include <linux/ioport.h>
  39. +#include <linux/of.h>
  40. +#include <linux/of_device.h>
  41. +#include <linux/of_platform.h>
  42. +#include <linux/ide.h>
  43. +
  44. +#define DRV_DESC "IDE driver for Magicbox 2.0 onboard CF slot"
  45. +#define DRV_NAME "magicbox_cf"
  46. +
  47. +static u8 magicbox_ide_inb(unsigned long port)
  48. +{
  49. + return (u8) (readw((void __iomem *) port) >> 8) & 0xff;
  50. +}
  51. +
  52. +static void magicbox_ide_outb(u8 value, unsigned long port)
  53. +{
  54. + writew(value << 8, (void __iomem *) port);
  55. +}
  56. +
  57. +static inline void magicbox_ide_insw(unsigned long port, void *addr, u32 count)
  58. +{
  59. + u16 *ptr;
  60. +
  61. + for (ptr = addr; count--; ptr++)
  62. + *ptr = readw((void __iomem *) port);
  63. +}
  64. +
  65. +static inline void magicbox_ide_insl(unsigned long port, void *addr, u32 count)
  66. +{
  67. + u32 *ptr;
  68. +
  69. + for (ptr = addr; count--; ptr++)
  70. + *ptr = readl((void __iomem *) port);
  71. +}
  72. +
  73. +static inline void magicbox_ide_outsw(unsigned long port, void *addr,
  74. + u32 count)
  75. +{
  76. + u16 *ptr;
  77. +
  78. + for (ptr = addr; count--; ptr++)
  79. + writew(*ptr, (void __iomem *) port);
  80. +}
  81. +
  82. +static inline void magicbox_ide_outsl(unsigned long port, void *addr,
  83. + u32 count)
  84. +{
  85. + u32 *ptr;
  86. +
  87. + for (ptr = addr; count--; ptr++)
  88. + writel(*ptr, (void __iomem *) port);
  89. +}
  90. +
  91. +static void magicbox_ide_exec_command(ide_hwif_t *hwif, u8 cmd)
  92. +{
  93. + magicbox_ide_outb(cmd, hwif->io_ports.command_addr);
  94. +}
  95. +
  96. +static u8 magicbox_ide_read_status(ide_hwif_t *hwif)
  97. +{
  98. + return magicbox_ide_inb(hwif->io_ports.status_addr);
  99. +}
  100. +
  101. +static u8 magicbox_ide_read_altstatus(ide_hwif_t *hwif)
  102. +{
  103. + return magicbox_ide_inb(hwif->io_ports.ctl_addr);
  104. +}
  105. +
  106. +static void magicbox_ide_tf_load(ide_drive_t *drive, ide_task_t *task)
  107. +{
  108. + struct ide_io_ports *io_ports = &drive->hwif->io_ports;
  109. + struct ide_taskfile *tf = &task->tf;
  110. + u8 HIHI = (task->tf_flags & IDE_TFLAG_LBA48) ? 0xE0 : 0xEF;
  111. +
  112. + if (task->tf_flags & IDE_TFLAG_FLAGGED)
  113. + HIHI = 0xFF;
  114. +
  115. + if (task->tf_flags & IDE_TFLAG_OUT_DATA)
  116. + writel((tf->hob_data << 8) | tf->data,
  117. + (void __iomem *) io_ports->data_addr);
  118. +
  119. + if (task->tf_flags & IDE_TFLAG_OUT_HOB_FEATURE)
  120. + magicbox_ide_outb(tf->hob_feature, io_ports->feature_addr);
  121. + if (task->tf_flags & IDE_TFLAG_OUT_HOB_NSECT)
  122. + magicbox_ide_outb(tf->hob_nsect, io_ports->nsect_addr);
  123. + if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAL)
  124. + magicbox_ide_outb(tf->hob_lbal, io_ports->lbal_addr);
  125. + if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAM)
  126. + magicbox_ide_outb(tf->hob_lbam, io_ports->lbam_addr);
  127. + if (task->tf_flags & IDE_TFLAG_OUT_HOB_LBAH)
  128. + magicbox_ide_outb(tf->hob_lbah, io_ports->lbah_addr);
  129. +
  130. + if (task->tf_flags & IDE_TFLAG_OUT_FEATURE)
  131. + magicbox_ide_outb(tf->feature, io_ports->feature_addr);
  132. + if (task->tf_flags & IDE_TFLAG_OUT_NSECT)
  133. + magicbox_ide_outb(tf->nsect, io_ports->nsect_addr);
  134. + if (task->tf_flags & IDE_TFLAG_OUT_LBAL)
  135. + magicbox_ide_outb(tf->lbal, io_ports->lbal_addr);
  136. + if (task->tf_flags & IDE_TFLAG_OUT_LBAM)
  137. + magicbox_ide_outb(tf->lbam, io_ports->lbam_addr);
  138. + if (task->tf_flags & IDE_TFLAG_OUT_LBAH)
  139. + magicbox_ide_outb(tf->lbah, io_ports->lbah_addr);
  140. +
  141. + if (task->tf_flags & IDE_TFLAG_OUT_DEVICE)
  142. + magicbox_ide_outb((tf->device & HIHI) | drive->select,
  143. + io_ports->device_addr);
  144. +}
  145. +
  146. +static void magicbox_ide_tf_read(ide_drive_t *drive, ide_task_t *task)
  147. +{
  148. + struct ide_io_ports *io_ports = &drive->hwif->io_ports;
  149. + struct ide_taskfile *tf = &task->tf;
  150. +
  151. + if (task->tf_flags & IDE_TFLAG_IN_DATA) {
  152. + u16 data = (u16) readl((void __iomem *) io_ports->data_addr);
  153. +
  154. + tf->data = data & 0xff;
  155. + tf->hob_data = (data >> 8) & 0xff;
  156. + }
  157. +
  158. + /* be sure we're looking at the low order bits */
  159. + magicbox_ide_outb(ATA_DEVCTL_OBS & ~0x80, io_ports->ctl_addr);
  160. +
  161. + if (task->tf_flags & IDE_TFLAG_IN_NSECT)
  162. + tf->nsect = magicbox_ide_inb(io_ports->nsect_addr);
  163. + if (task->tf_flags & IDE_TFLAG_IN_LBAL)
  164. + tf->lbal = magicbox_ide_inb(io_ports->lbal_addr);
  165. + if (task->tf_flags & IDE_TFLAG_IN_LBAM)
  166. + tf->lbam = magicbox_ide_inb(io_ports->lbam_addr);
  167. + if (task->tf_flags & IDE_TFLAG_IN_LBAH)
  168. + tf->lbah = magicbox_ide_inb(io_ports->lbah_addr);
  169. + if (task->tf_flags & IDE_TFLAG_IN_DEVICE)
  170. + tf->device = magicbox_ide_inb(io_ports->device_addr);
  171. +
  172. + if (task->tf_flags & IDE_TFLAG_LBA48) {
  173. + magicbox_ide_outb(ATA_DEVCTL_OBS | 0x80, io_ports->ctl_addr);
  174. +
  175. + if (task->tf_flags & IDE_TFLAG_IN_HOB_FEATURE)
  176. + tf->hob_feature = magicbox_ide_inb(io_ports->feature_addr);
  177. + if (task->tf_flags & IDE_TFLAG_IN_HOB_NSECT)
  178. + tf->hob_nsect = magicbox_ide_inb(io_ports->nsect_addr);
  179. + if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAL)
  180. + tf->hob_lbal = magicbox_ide_inb(io_ports->lbal_addr);
  181. + if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAM)
  182. + tf->hob_lbam = magicbox_ide_inb(io_ports->lbam_addr);
  183. + if (task->tf_flags & IDE_TFLAG_IN_HOB_LBAH)
  184. + tf->hob_lbah = magicbox_ide_inb(io_ports->lbah_addr);
  185. + }
  186. +}
  187. +
  188. +static void magicbox_ide_input_data(ide_drive_t *drive, struct request *rq,
  189. + void *buf, unsigned int len)
  190. +{
  191. + unsigned long port = drive->hwif->io_ports.data_addr;
  192. +
  193. + len++;
  194. +
  195. + if (drive->io_32bit) {
  196. + magicbox_ide_insl(port, buf, len / 4);
  197. +
  198. + if ((len & 3) >= 2)
  199. + magicbox_ide_insw(port, (u8 *)buf + (len & ~3), 1);
  200. + } else
  201. + magicbox_ide_insw(port, buf, len / 2);
  202. +}
  203. +
  204. +static void magicbox_ide_output_data(ide_drive_t *drive, struct request *rq,
  205. + void *buf, unsigned int len)
  206. +{
  207. + unsigned long port = drive->hwif->io_ports.data_addr;
  208. +
  209. + len++;
  210. +
  211. + if (drive->io_32bit) {
  212. + magicbox_ide_outsl(port, buf, len / 4);
  213. +
  214. + if ((len & 3) >= 2)
  215. + magicbox_ide_outsw(port, (u8 *)buf + (len & ~3), 1);
  216. + } else
  217. + magicbox_ide_outsw(port, buf, len / 2);
  218. +}
  219. +
  220. +static void magicbox_ide_set_pio_mode(ide_drive_t *drive, const u8 pio)
  221. +{
  222. +}
  223. +
  224. +static u8 magicbox_ide_cable_detect(ide_hwif_t *hwif)
  225. +{
  226. + return ATA_CBL_PATA40;
  227. +}
  228. +
  229. +static const struct ide_tp_ops magicbox_ide_tp_ops = {
  230. + .exec_command = magicbox_ide_exec_command,
  231. + .read_status = magicbox_ide_read_status,
  232. + .read_altstatus = magicbox_ide_read_altstatus,
  233. +
  234. + .set_irq = ide_set_irq,
  235. + .tf_load = magicbox_ide_tf_load,
  236. + .tf_read = magicbox_ide_tf_read,
  237. +
  238. + .input_data = magicbox_ide_input_data,
  239. + .output_data = magicbox_ide_output_data,
  240. +};
  241. +
  242. +static const struct ide_port_ops magicbox_ide_port_ops = {
  243. + .set_pio_mode = magicbox_ide_set_pio_mode,
  244. + .cable_detect = magicbox_ide_cable_detect,
  245. +};
  246. +
  247. +static const struct ide_port_info magicbox_ide_port_info = {
  248. + .name = DRV_NAME,
  249. + .chipset = ide_generic,
  250. + .tp_ops = &magicbox_ide_tp_ops,
  251. + .port_ops = &magicbox_ide_port_ops,
  252. + .host_flags = IDE_HFLAG_SINGLE |
  253. + IDE_HFLAG_NO_DMA |
  254. + IDE_HFLAG_MMIO |
  255. + IDE_HFLAG_UNMASK_IRQS,
  256. + .pio_mask = ATA_PIO4,
  257. +};
  258. +
  259. +static inline void magicbox_ide_setup_hw(hw_regs_t *hw, u16 __iomem *base,
  260. + u16 __iomem *ctrl, int irq)
  261. +{
  262. + unsigned long port = (unsigned long) base;
  263. + int i;
  264. +
  265. + memset(hw, 0, sizeof(*hw));
  266. + for (i = 0; i <= 7; i++)
  267. + hw->io_ports_array[i] = port + i * 2;
  268. +
  269. + /*
  270. + * the IDE control register is at ATA address 6,
  271. + * with CS1 active instead of CS0
  272. + */
  273. + hw->io_ports.ctl_addr = (unsigned long)ctrl + (6 * 2);
  274. +
  275. + hw->irq = irq;
  276. + hw->chipset = ide_generic;
  277. + hw->ack_intr = NULL;
  278. +}
  279. +
  280. +static int __devinit magicbox_ide_of_probe(struct of_device *op,
  281. + const struct of_device_id *match)
  282. +{
  283. + hw_regs_t hw;
  284. + hw_regs_t *hws[] = { &hw, NULL, NULL, NULL };
  285. + struct ide_host *host;
  286. + u16 __iomem *base;
  287. + u16 __iomem *ctrl;
  288. + int irq;
  289. + int ret = 0;
  290. +
  291. + irq = irq_of_parse_and_map(op->node, 0);
  292. + if (irq < 0) {
  293. + dev_err(&op->dev, "invalid irq\n");
  294. + ret = -EINVAL;
  295. + goto err_exit;
  296. + }
  297. +
  298. + base = of_iomap(op->node, 0);
  299. + if (base == NULL) {
  300. + ret = -ENOMEM;
  301. + goto err_exit;
  302. + }
  303. +
  304. + ctrl = of_iomap(op->node, 1);
  305. + if (ctrl == NULL) {
  306. + ret = -ENOMEM;
  307. + goto err_unmap_base;
  308. + }
  309. +
  310. + magicbox_ide_setup_hw(&hw, base, ctrl, irq);
  311. +
  312. + hw.dev = &op->dev;
  313. +
  314. + ret = ide_host_add(&magicbox_ide_port_info, hws, &host);
  315. + if (ret)
  316. + goto err_unmap_ctrl;
  317. +
  318. + dev_set_drvdata(&op->dev, host);
  319. +
  320. + return 0;
  321. +
  322. + err_unmap_ctrl:
  323. + iounmap(ctrl);
  324. + err_unmap_base:
  325. + iounmap(base);
  326. + err_exit:
  327. + return ret;
  328. +}
  329. +
  330. +static struct of_device_id magicbox_ide_of_match[] = {
  331. + { .compatible = "magicbox-cf", },
  332. + {},
  333. +};
  334. +
  335. +static struct of_platform_driver magicbox_ide_of_platform_driver = {
  336. + .owner = THIS_MODULE,
  337. + .name = DRV_NAME,
  338. + .match_table = magicbox_ide_of_match,
  339. + .probe = magicbox_ide_of_probe,
  340. + .driver = {
  341. + .name = DRV_NAME,
  342. + .owner = THIS_MODULE,
  343. + },
  344. +};
  345. +
  346. +static int __init magicbox_ide_init(void)
  347. +{
  348. + return of_register_platform_driver(&magicbox_ide_of_platform_driver);
  349. +}
  350. +
  351. +module_init(magicbox_ide_init);
  352. +
  353. +MODULE_DESCRIPTION(DRV_DESC);
  354. +MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  355. +MODULE_LICENSE("GPL v2");
  356. +MODULE_DEVICE_TABLE(of, magicbox_ide_of_match);