palm_bk3710.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Palmchip bk3710 IDE controller
  3. *
  4. * Copyright (C) 2006 Texas Instruments.
  5. * Copyright (C) 2007 MontaVista Software, Inc., <source@mvista.com>
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. * ----------------------------------------------------------------------------
  23. *
  24. */
  25. #include <linux/types.h>
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/ioport.h>
  29. #include <linux/ide.h>
  30. #include <linux/delay.h>
  31. #include <linux/init.h>
  32. #include <linux/clk.h>
  33. #include <linux/platform_device.h>
  34. /* Offset of the primary interface registers */
  35. #define IDE_PALM_ATA_PRI_REG_OFFSET 0x1F0
  36. /* Primary Control Offset */
  37. #define IDE_PALM_ATA_PRI_CTL_OFFSET 0x3F6
  38. #define BK3710_BMICP 0x00
  39. #define BK3710_BMISP 0x02
  40. #define BK3710_BMIDTP 0x04
  41. #define BK3710_IDETIMP 0x40
  42. #define BK3710_IDESTATUS 0x47
  43. #define BK3710_UDMACTL 0x48
  44. #define BK3710_MISCCTL 0x50
  45. #define BK3710_REGSTB 0x54
  46. #define BK3710_REGRCVR 0x58
  47. #define BK3710_DATSTB 0x5C
  48. #define BK3710_DATRCVR 0x60
  49. #define BK3710_DMASTB 0x64
  50. #define BK3710_DMARCVR 0x68
  51. #define BK3710_UDMASTB 0x6C
  52. #define BK3710_UDMATRP 0x70
  53. #define BK3710_UDMAENV 0x74
  54. #define BK3710_IORDYTMP 0x78
  55. static unsigned ideclk_period; /* in nanoseconds */
  56. struct palm_bk3710_udmatiming {
  57. unsigned int rptime; /* tRP -- Ready to pause time (nsec) */
  58. unsigned int cycletime; /* tCYCTYP2/2 -- avg Cycle Time (nsec) */
  59. /* tENV is always a minimum of 20 nsec */
  60. };
  61. static const struct palm_bk3710_udmatiming palm_bk3710_udmatimings[6] = {
  62. { 160, 240 / 2 }, /* UDMA Mode 0 */
  63. { 125, 160 / 2 }, /* UDMA Mode 1 */
  64. { 100, 120 / 2 }, /* UDMA Mode 2 */
  65. { 100, 90 / 2 }, /* UDMA Mode 3 */
  66. { 100, 60 / 2 }, /* UDMA Mode 4 */
  67. { 85, 40 / 2 }, /* UDMA Mode 5 */
  68. };
  69. static void palm_bk3710_setudmamode(void __iomem *base, unsigned int dev,
  70. unsigned int mode)
  71. {
  72. u8 tenv, trp, t0;
  73. u32 val32;
  74. u16 val16;
  75. /* DMA Data Setup */
  76. t0 = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].cycletime,
  77. ideclk_period) - 1;
  78. tenv = DIV_ROUND_UP(20, ideclk_period) - 1;
  79. trp = DIV_ROUND_UP(palm_bk3710_udmatimings[mode].rptime,
  80. ideclk_period) - 1;
  81. /* udmastb Ultra DMA Access Strobe Width */
  82. val32 = readl(base + BK3710_UDMASTB) & (0xFF << (dev ? 0 : 8));
  83. val32 |= (t0 << (dev ? 8 : 0));
  84. writel(val32, base + BK3710_UDMASTB);
  85. /* udmatrp Ultra DMA Ready to Pause Time */
  86. val32 = readl(base + BK3710_UDMATRP) & (0xFF << (dev ? 0 : 8));
  87. val32 |= (trp << (dev ? 8 : 0));
  88. writel(val32, base + BK3710_UDMATRP);
  89. /* udmaenv Ultra DMA envelop Time */
  90. val32 = readl(base + BK3710_UDMAENV) & (0xFF << (dev ? 0 : 8));
  91. val32 |= (tenv << (dev ? 8 : 0));
  92. writel(val32, base + BK3710_UDMAENV);
  93. /* Enable UDMA for Device */
  94. val16 = readw(base + BK3710_UDMACTL) | (1 << dev);
  95. writew(val16, base + BK3710_UDMACTL);
  96. }
  97. static void palm_bk3710_setdmamode(void __iomem *base, unsigned int dev,
  98. unsigned short min_cycle,
  99. unsigned int mode)
  100. {
  101. u8 td, tkw, t0;
  102. u32 val32;
  103. u16 val16;
  104. struct ide_timing *t;
  105. int cycletime;
  106. t = ide_timing_find_mode(mode);
  107. cycletime = max_t(int, t->cycle, min_cycle);
  108. /* DMA Data Setup */
  109. t0 = DIV_ROUND_UP(cycletime, ideclk_period);
  110. td = DIV_ROUND_UP(t->active, ideclk_period);
  111. tkw = t0 - td - 1;
  112. td -= 1;
  113. val32 = readl(base + BK3710_DMASTB) & (0xFF << (dev ? 0 : 8));
  114. val32 |= (td << (dev ? 8 : 0));
  115. writel(val32, base + BK3710_DMASTB);
  116. val32 = readl(base + BK3710_DMARCVR) & (0xFF << (dev ? 0 : 8));
  117. val32 |= (tkw << (dev ? 8 : 0));
  118. writel(val32, base + BK3710_DMARCVR);
  119. /* Disable UDMA for Device */
  120. val16 = readw(base + BK3710_UDMACTL) & ~(1 << dev);
  121. writew(val16, base + BK3710_UDMACTL);
  122. }
  123. static void palm_bk3710_setpiomode(void __iomem *base, ide_drive_t *mate,
  124. unsigned int dev, unsigned int cycletime,
  125. unsigned int mode)
  126. {
  127. u8 t2, t2i, t0;
  128. u32 val32;
  129. struct ide_timing *t;
  130. t = ide_timing_find_mode(XFER_PIO_0 + mode);
  131. /* PIO Data Setup */
  132. t0 = DIV_ROUND_UP(cycletime, ideclk_period);
  133. t2 = DIV_ROUND_UP(t->active, ideclk_period);
  134. t2i = t0 - t2 - 1;
  135. t2 -= 1;
  136. val32 = readl(base + BK3710_DATSTB) & (0xFF << (dev ? 0 : 8));
  137. val32 |= (t2 << (dev ? 8 : 0));
  138. writel(val32, base + BK3710_DATSTB);
  139. val32 = readl(base + BK3710_DATRCVR) & (0xFF << (dev ? 0 : 8));
  140. val32 |= (t2i << (dev ? 8 : 0));
  141. writel(val32, base + BK3710_DATRCVR);
  142. if (mate) {
  143. u8 mode2 = mate->pio_mode - XFER_PIO_0;
  144. if (mode2 < mode)
  145. mode = mode2;
  146. }
  147. /* TASKFILE Setup */
  148. t0 = DIV_ROUND_UP(t->cyc8b, ideclk_period);
  149. t2 = DIV_ROUND_UP(t->act8b, ideclk_period);
  150. t2i = t0 - t2 - 1;
  151. t2 -= 1;
  152. val32 = readl(base + BK3710_REGSTB) & (0xFF << (dev ? 0 : 8));
  153. val32 |= (t2 << (dev ? 8 : 0));
  154. writel(val32, base + BK3710_REGSTB);
  155. val32 = readl(base + BK3710_REGRCVR) & (0xFF << (dev ? 0 : 8));
  156. val32 |= (t2i << (dev ? 8 : 0));
  157. writel(val32, base + BK3710_REGRCVR);
  158. }
  159. static void palm_bk3710_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  160. {
  161. int is_slave = drive->dn & 1;
  162. void __iomem *base = (void __iomem *)hwif->dma_base;
  163. const u8 xferspeed = drive->dma_mode;
  164. if (xferspeed >= XFER_UDMA_0) {
  165. palm_bk3710_setudmamode(base, is_slave,
  166. xferspeed - XFER_UDMA_0);
  167. } else {
  168. palm_bk3710_setdmamode(base, is_slave,
  169. drive->id[ATA_ID_EIDE_DMA_MIN],
  170. xferspeed);
  171. }
  172. }
  173. static void palm_bk3710_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
  174. {
  175. unsigned int cycle_time;
  176. int is_slave = drive->dn & 1;
  177. ide_drive_t *mate;
  178. void __iomem *base = (void __iomem *)hwif->dma_base;
  179. const u8 pio = drive->pio_mode - XFER_PIO_0;
  180. /*
  181. * Obtain the drive PIO data for tuning the Palm Chip registers
  182. */
  183. cycle_time = ide_pio_cycle_time(drive, pio);
  184. mate = ide_get_pair_dev(drive);
  185. palm_bk3710_setpiomode(base, mate, is_slave, cycle_time, pio);
  186. }
  187. static void palm_bk3710_chipinit(void __iomem *base)
  188. {
  189. /*
  190. * REVISIT: the ATA reset signal needs to be managed through a
  191. * GPIO, which means it should come from platform_data. Until
  192. * we get and use such information, we have to trust that things
  193. * have been reset before we get here.
  194. */
  195. /*
  196. * Program the IDETIMP Register Value based on the following assumptions
  197. *
  198. * (ATA_IDETIMP_IDEEN , ENABLE ) |
  199. * (ATA_IDETIMP_PREPOST1 , DISABLE) |
  200. * (ATA_IDETIMP_PREPOST0 , DISABLE) |
  201. *
  202. * DM6446 silicon rev 2.1 and earlier have no observed net benefit
  203. * from enabling prefetch/postwrite.
  204. */
  205. writew(BIT(15), base + BK3710_IDETIMP);
  206. /*
  207. * UDMACTL Ultra-ATA DMA Control
  208. * (ATA_UDMACTL_UDMAP1 , 0 ) |
  209. * (ATA_UDMACTL_UDMAP0 , 0 )
  210. *
  211. */
  212. writew(0, base + BK3710_UDMACTL);
  213. /*
  214. * MISCCTL Miscellaneous Conrol Register
  215. * (ATA_MISCCTL_HWNHLD1P , 1 cycle)
  216. * (ATA_MISCCTL_HWNHLD0P , 1 cycle)
  217. * (ATA_MISCCTL_TIMORIDE , 1)
  218. */
  219. writel(0x001, base + BK3710_MISCCTL);
  220. /*
  221. * IORDYTMP IORDY Timer for Primary Register
  222. * (ATA_IORDYTMP_IORDYTMP , 0xffff )
  223. */
  224. writel(0xFFFF, base + BK3710_IORDYTMP);
  225. /*
  226. * Configure BMISP Register
  227. * (ATA_BMISP_DMAEN1 , DISABLE ) |
  228. * (ATA_BMISP_DMAEN0 , DISABLE ) |
  229. * (ATA_BMISP_IORDYINT , CLEAR) |
  230. * (ATA_BMISP_INTRSTAT , CLEAR) |
  231. * (ATA_BMISP_DMAERROR , CLEAR)
  232. */
  233. writew(0, base + BK3710_BMISP);
  234. palm_bk3710_setpiomode(base, NULL, 0, 600, 0);
  235. palm_bk3710_setpiomode(base, NULL, 1, 600, 0);
  236. }
  237. static u8 palm_bk3710_cable_detect(ide_hwif_t *hwif)
  238. {
  239. return ATA_CBL_PATA80;
  240. }
  241. static int palm_bk3710_init_dma(ide_hwif_t *hwif, const struct ide_port_info *d)
  242. {
  243. printk(KERN_INFO " %s: MMIO-DMA\n", hwif->name);
  244. if (ide_allocate_dma_engine(hwif))
  245. return -1;
  246. hwif->dma_base = hwif->io_ports.data_addr - IDE_PALM_ATA_PRI_REG_OFFSET;
  247. return 0;
  248. }
  249. static const struct ide_port_ops palm_bk3710_ports_ops = {
  250. .set_pio_mode = palm_bk3710_set_pio_mode,
  251. .set_dma_mode = palm_bk3710_set_dma_mode,
  252. .cable_detect = palm_bk3710_cable_detect,
  253. };
  254. static struct ide_port_info palm_bk3710_port_info __initdata = {
  255. .init_dma = palm_bk3710_init_dma,
  256. .port_ops = &palm_bk3710_ports_ops,
  257. .dma_ops = &sff_dma_ops,
  258. .host_flags = IDE_HFLAG_MMIO,
  259. .pio_mask = ATA_PIO4,
  260. .mwdma_mask = ATA_MWDMA2,
  261. .chipset = ide_palm3710,
  262. };
  263. static int __init palm_bk3710_probe(struct platform_device *pdev)
  264. {
  265. struct clk *clk;
  266. struct resource *mem, *irq;
  267. void __iomem *base;
  268. unsigned long rate, mem_size;
  269. int i, rc;
  270. struct ide_hw hw, *hws[] = { &hw };
  271. clk = clk_get(&pdev->dev, NULL);
  272. if (IS_ERR(clk))
  273. return -ENODEV;
  274. clk_enable(clk);
  275. rate = clk_get_rate(clk);
  276. if (!rate)
  277. return -EINVAL;
  278. /* NOTE: round *down* to meet minimum timings; we count in clocks */
  279. ideclk_period = 1000000000UL / rate;
  280. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  281. if (mem == NULL) {
  282. printk(KERN_ERR "failed to get memory region resource\n");
  283. return -ENODEV;
  284. }
  285. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  286. if (irq == NULL) {
  287. printk(KERN_ERR "failed to get IRQ resource\n");
  288. return -ENODEV;
  289. }
  290. mem_size = resource_size(mem);
  291. if (request_mem_region(mem->start, mem_size, "palm_bk3710") == NULL) {
  292. printk(KERN_ERR "failed to request memory region\n");
  293. return -EBUSY;
  294. }
  295. base = ioremap(mem->start, mem_size);
  296. if (!base) {
  297. printk(KERN_ERR "failed to map IO memory\n");
  298. release_mem_region(mem->start, mem_size);
  299. return -ENOMEM;
  300. }
  301. /* Configure the Palm Chip controller */
  302. palm_bk3710_chipinit(base);
  303. memset(&hw, 0, sizeof(hw));
  304. for (i = 0; i < IDE_NR_PORTS - 2; i++)
  305. hw.io_ports_array[i] = (unsigned long)
  306. (base + IDE_PALM_ATA_PRI_REG_OFFSET + i);
  307. hw.io_ports.ctl_addr = (unsigned long)
  308. (base + IDE_PALM_ATA_PRI_CTL_OFFSET);
  309. hw.irq = irq->start;
  310. hw.dev = &pdev->dev;
  311. palm_bk3710_port_info.udma_mask = rate < 100000000 ? ATA_UDMA4 :
  312. ATA_UDMA5;
  313. /* Register the IDE interface with Linux */
  314. rc = ide_host_add(&palm_bk3710_port_info, hws, 1, NULL);
  315. if (rc)
  316. goto out;
  317. return 0;
  318. out:
  319. printk(KERN_WARNING "Palm Chip BK3710 IDE Register Fail\n");
  320. return rc;
  321. }
  322. /* work with hotplug and coldplug */
  323. MODULE_ALIAS("platform:palm_bk3710");
  324. static struct platform_driver platform_bk_driver = {
  325. .driver = {
  326. .name = "palm_bk3710",
  327. },
  328. };
  329. static int __init palm_bk3710_init(void)
  330. {
  331. return platform_driver_probe(&platform_bk_driver, palm_bk3710_probe);
  332. }
  333. module_init(palm_bk3710_init);
  334. MODULE_LICENSE("GPL");