palm_bk3710.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 = ide_get_best_pio_mode(mate, 255, 4);
  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_drive_t *drive, u8 xferspeed)
  160. {
  161. int is_slave = drive->dn & 1;
  162. void __iomem *base = (void *)drive->hwif->dma_base;
  163. if (xferspeed >= XFER_UDMA_0) {
  164. palm_bk3710_setudmamode(base, is_slave,
  165. xferspeed - XFER_UDMA_0);
  166. } else {
  167. palm_bk3710_setdmamode(base, is_slave,
  168. drive->id[ATA_ID_EIDE_DMA_MIN],
  169. xferspeed);
  170. }
  171. }
  172. static void palm_bk3710_set_pio_mode(ide_drive_t *drive, u8 pio)
  173. {
  174. unsigned int cycle_time;
  175. int is_slave = drive->dn & 1;
  176. ide_drive_t *mate;
  177. void __iomem *base = (void *)drive->hwif->dma_base;
  178. /*
  179. * Obtain the drive PIO data for tuning the Palm Chip registers
  180. */
  181. cycle_time = ide_pio_cycle_time(drive, pio);
  182. mate = ide_get_pair_dev(drive);
  183. palm_bk3710_setpiomode(base, mate, is_slave, cycle_time, pio);
  184. }
  185. static void __devinit palm_bk3710_chipinit(void __iomem *base)
  186. {
  187. /*
  188. * REVISIT: the ATA reset signal needs to be managed through a
  189. * GPIO, which means it should come from platform_data. Until
  190. * we get and use such information, we have to trust that things
  191. * have been reset before we get here.
  192. */
  193. /*
  194. * Program the IDETIMP Register Value based on the following assumptions
  195. *
  196. * (ATA_IDETIMP_IDEEN , ENABLE ) |
  197. * (ATA_IDETIMP_PREPOST1 , DISABLE) |
  198. * (ATA_IDETIMP_PREPOST0 , DISABLE) |
  199. *
  200. * DM6446 silicon rev 2.1 and earlier have no observed net benefit
  201. * from enabling prefetch/postwrite.
  202. */
  203. writew(BIT(15), base + BK3710_IDETIMP);
  204. /*
  205. * UDMACTL Ultra-ATA DMA Control
  206. * (ATA_UDMACTL_UDMAP1 , 0 ) |
  207. * (ATA_UDMACTL_UDMAP0 , 0 )
  208. *
  209. */
  210. writew(0, base + BK3710_UDMACTL);
  211. /*
  212. * MISCCTL Miscellaneous Conrol Register
  213. * (ATA_MISCCTL_HWNHLD1P , 1 cycle)
  214. * (ATA_MISCCTL_HWNHLD0P , 1 cycle)
  215. * (ATA_MISCCTL_TIMORIDE , 1)
  216. */
  217. writel(0x001, base + BK3710_MISCCTL);
  218. /*
  219. * IORDYTMP IORDY Timer for Primary Register
  220. * (ATA_IORDYTMP_IORDYTMP , 0xffff )
  221. */
  222. writel(0xFFFF, base + BK3710_IORDYTMP);
  223. /*
  224. * Configure BMISP Register
  225. * (ATA_BMISP_DMAEN1 , DISABLE ) |
  226. * (ATA_BMISP_DMAEN0 , DISABLE ) |
  227. * (ATA_BMISP_IORDYINT , CLEAR) |
  228. * (ATA_BMISP_INTRSTAT , CLEAR) |
  229. * (ATA_BMISP_DMAERROR , CLEAR)
  230. */
  231. writew(0, base + BK3710_BMISP);
  232. palm_bk3710_setpiomode(base, NULL, 0, 600, 0);
  233. palm_bk3710_setpiomode(base, NULL, 1, 600, 0);
  234. }
  235. static u8 palm_bk3710_cable_detect(ide_hwif_t *hwif)
  236. {
  237. return ATA_CBL_PATA80;
  238. }
  239. static int __devinit palm_bk3710_init_dma(ide_hwif_t *hwif,
  240. const struct ide_port_info *d)
  241. {
  242. printk(KERN_INFO " %s: MMIO-DMA\n", hwif->name);
  243. if (ide_allocate_dma_engine(hwif))
  244. return -1;
  245. hwif->dma_base = hwif->io_ports.data_addr - IDE_PALM_ATA_PRI_REG_OFFSET;
  246. return 0;
  247. }
  248. static const struct ide_port_ops palm_bk3710_ports_ops = {
  249. .set_pio_mode = palm_bk3710_set_pio_mode,
  250. .set_dma_mode = palm_bk3710_set_dma_mode,
  251. .cable_detect = palm_bk3710_cable_detect,
  252. };
  253. static struct ide_port_info __devinitdata palm_bk3710_port_info = {
  254. .init_dma = palm_bk3710_init_dma,
  255. .port_ops = &palm_bk3710_ports_ops,
  256. .dma_ops = &sff_dma_ops,
  257. .host_flags = IDE_HFLAG_MMIO,
  258. .pio_mask = ATA_PIO4,
  259. .mwdma_mask = ATA_MWDMA2,
  260. .chipset = ide_palm3710,
  261. };
  262. static int __init palm_bk3710_probe(struct platform_device *pdev)
  263. {
  264. struct clk *clk;
  265. struct resource *mem, *irq;
  266. void __iomem *base;
  267. unsigned long rate, mem_size;
  268. int i, rc;
  269. struct ide_hw hw, *hws[] = { &hw };
  270. clk = clk_get(&pdev->dev, "IDECLK");
  271. if (IS_ERR(clk))
  272. return -ENODEV;
  273. clk_enable(clk);
  274. rate = clk_get_rate(clk);
  275. /* NOTE: round *down* to meet minimum timings; we count in clocks */
  276. ideclk_period = 1000000000UL / rate;
  277. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  278. if (mem == NULL) {
  279. printk(KERN_ERR "failed to get memory region resource\n");
  280. return -ENODEV;
  281. }
  282. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  283. if (irq == NULL) {
  284. printk(KERN_ERR "failed to get IRQ resource\n");
  285. return -ENODEV;
  286. }
  287. mem_size = mem->end - mem->start + 1;
  288. if (request_mem_region(mem->start, mem_size, "palm_bk3710") == NULL) {
  289. printk(KERN_ERR "failed to request memory region\n");
  290. return -EBUSY;
  291. }
  292. base = ioremap(mem->start, mem_size);
  293. if (!base) {
  294. printk(KERN_ERR "failed to map IO memory\n");
  295. release_mem_region(mem->start, mem_size);
  296. return -ENOMEM;
  297. }
  298. /* Configure the Palm Chip controller */
  299. palm_bk3710_chipinit(base);
  300. memset(&hw, 0, sizeof(hw));
  301. for (i = 0; i < IDE_NR_PORTS - 2; i++)
  302. hw.io_ports_array[i] = (unsigned long)
  303. (base + IDE_PALM_ATA_PRI_REG_OFFSET + i);
  304. hw.io_ports.ctl_addr = (unsigned long)
  305. (base + IDE_PALM_ATA_PRI_CTL_OFFSET);
  306. hw.irq = irq->start;
  307. hw.dev = &pdev->dev;
  308. palm_bk3710_port_info.udma_mask = rate < 100000000 ? ATA_UDMA4 :
  309. ATA_UDMA5;
  310. /* Register the IDE interface with Linux */
  311. rc = ide_host_add(&palm_bk3710_port_info, hws, 1, NULL);
  312. if (rc)
  313. goto out;
  314. return 0;
  315. out:
  316. printk(KERN_WARNING "Palm Chip BK3710 IDE Register Fail\n");
  317. return rc;
  318. }
  319. /* work with hotplug and coldplug */
  320. MODULE_ALIAS("platform:palm_bk3710");
  321. static struct platform_driver platform_bk_driver = {
  322. .driver = {
  323. .name = "palm_bk3710",
  324. .owner = THIS_MODULE,
  325. },
  326. };
  327. static int __init palm_bk3710_init(void)
  328. {
  329. return platform_driver_probe(&platform_bk_driver, palm_bk3710_probe);
  330. }
  331. module_init(palm_bk3710_init);
  332. MODULE_LICENSE("GPL");