sm501.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. /* linux/drivers/mfd/sm501.c
  2. *
  3. * Copyright (C) 2006 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. * Vincent Sanders <vince@simtec.co.uk>
  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 version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * SM501 MFD driver
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <linux/list.h>
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pci.h>
  21. #include <linux/i2c-gpio.h>
  22. #include <linux/slab.h>
  23. #include <linux/sm501.h>
  24. #include <linux/sm501-regs.h>
  25. #include <linux/serial_8250.h>
  26. #include <linux/io.h>
  27. struct sm501_device {
  28. struct list_head list;
  29. struct platform_device pdev;
  30. };
  31. struct sm501_gpio;
  32. #ifdef CONFIG_MFD_SM501_GPIO
  33. #include <linux/gpio.h>
  34. struct sm501_gpio_chip {
  35. struct gpio_chip gpio;
  36. struct sm501_gpio *ourgpio; /* to get back to parent. */
  37. void __iomem *regbase;
  38. void __iomem *control; /* address of control reg. */
  39. };
  40. struct sm501_gpio {
  41. struct sm501_gpio_chip low;
  42. struct sm501_gpio_chip high;
  43. spinlock_t lock;
  44. unsigned int registered : 1;
  45. void __iomem *regs;
  46. struct resource *regs_res;
  47. };
  48. #else
  49. struct sm501_gpio {
  50. /* no gpio support, empty definition for sm501_devdata. */
  51. };
  52. #endif
  53. struct sm501_devdata {
  54. spinlock_t reg_lock;
  55. struct mutex clock_lock;
  56. struct list_head devices;
  57. struct sm501_gpio gpio;
  58. struct device *dev;
  59. struct resource *io_res;
  60. struct resource *mem_res;
  61. struct resource *regs_claim;
  62. struct sm501_platdata *platdata;
  63. unsigned int in_suspend;
  64. unsigned long pm_misc;
  65. int unit_power[20];
  66. unsigned int pdev_id;
  67. unsigned int irq;
  68. void __iomem *regs;
  69. unsigned int rev;
  70. };
  71. #define MHZ (1000 * 1000)
  72. #ifdef DEBUG
  73. static const unsigned int div_tab[] = {
  74. [0] = 1,
  75. [1] = 2,
  76. [2] = 4,
  77. [3] = 8,
  78. [4] = 16,
  79. [5] = 32,
  80. [6] = 64,
  81. [7] = 128,
  82. [8] = 3,
  83. [9] = 6,
  84. [10] = 12,
  85. [11] = 24,
  86. [12] = 48,
  87. [13] = 96,
  88. [14] = 192,
  89. [15] = 384,
  90. [16] = 5,
  91. [17] = 10,
  92. [18] = 20,
  93. [19] = 40,
  94. [20] = 80,
  95. [21] = 160,
  96. [22] = 320,
  97. [23] = 604,
  98. };
  99. static unsigned long decode_div(unsigned long pll2, unsigned long val,
  100. unsigned int lshft, unsigned int selbit,
  101. unsigned long mask)
  102. {
  103. if (val & selbit)
  104. pll2 = 288 * MHZ;
  105. return pll2 / div_tab[(val >> lshft) & mask];
  106. }
  107. #define fmt_freq(x) ((x) / MHZ), ((x) % MHZ), (x)
  108. /* sm501_dump_clk
  109. *
  110. * Print out the current clock configuration for the device
  111. */
  112. static void sm501_dump_clk(struct sm501_devdata *sm)
  113. {
  114. unsigned long misct = smc501_readl(sm->regs + SM501_MISC_TIMING);
  115. unsigned long pm0 = smc501_readl(sm->regs + SM501_POWER_MODE_0_CLOCK);
  116. unsigned long pm1 = smc501_readl(sm->regs + SM501_POWER_MODE_1_CLOCK);
  117. unsigned long pmc = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
  118. unsigned long sdclk0, sdclk1;
  119. unsigned long pll2 = 0;
  120. switch (misct & 0x30) {
  121. case 0x00:
  122. pll2 = 336 * MHZ;
  123. break;
  124. case 0x10:
  125. pll2 = 288 * MHZ;
  126. break;
  127. case 0x20:
  128. pll2 = 240 * MHZ;
  129. break;
  130. case 0x30:
  131. pll2 = 192 * MHZ;
  132. break;
  133. }
  134. sdclk0 = (misct & (1<<12)) ? pll2 : 288 * MHZ;
  135. sdclk0 /= div_tab[((misct >> 8) & 0xf)];
  136. sdclk1 = (misct & (1<<20)) ? pll2 : 288 * MHZ;
  137. sdclk1 /= div_tab[((misct >> 16) & 0xf)];
  138. dev_dbg(sm->dev, "MISCT=%08lx, PM0=%08lx, PM1=%08lx\n",
  139. misct, pm0, pm1);
  140. dev_dbg(sm->dev, "PLL2 = %ld.%ld MHz (%ld), SDCLK0=%08lx, SDCLK1=%08lx\n",
  141. fmt_freq(pll2), sdclk0, sdclk1);
  142. dev_dbg(sm->dev, "SDRAM: PM0=%ld, PM1=%ld\n", sdclk0, sdclk1);
  143. dev_dbg(sm->dev, "PM0[%c]: "
  144. "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
  145. "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
  146. (pmc & 3 ) == 0 ? '*' : '-',
  147. fmt_freq(decode_div(pll2, pm0, 24, 1<<29, 31)),
  148. fmt_freq(decode_div(pll2, pm0, 16, 1<<20, 15)),
  149. fmt_freq(decode_div(pll2, pm0, 8, 1<<12, 15)),
  150. fmt_freq(decode_div(pll2, pm0, 0, 1<<4, 15)));
  151. dev_dbg(sm->dev, "PM1[%c]: "
  152. "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
  153. "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
  154. (pmc & 3 ) == 1 ? '*' : '-',
  155. fmt_freq(decode_div(pll2, pm1, 24, 1<<29, 31)),
  156. fmt_freq(decode_div(pll2, pm1, 16, 1<<20, 15)),
  157. fmt_freq(decode_div(pll2, pm1, 8, 1<<12, 15)),
  158. fmt_freq(decode_div(pll2, pm1, 0, 1<<4, 15)));
  159. }
  160. static void sm501_dump_regs(struct sm501_devdata *sm)
  161. {
  162. void __iomem *regs = sm->regs;
  163. dev_info(sm->dev, "System Control %08x\n",
  164. smc501_readl(regs + SM501_SYSTEM_CONTROL));
  165. dev_info(sm->dev, "Misc Control %08x\n",
  166. smc501_readl(regs + SM501_MISC_CONTROL));
  167. dev_info(sm->dev, "GPIO Control Low %08x\n",
  168. smc501_readl(regs + SM501_GPIO31_0_CONTROL));
  169. dev_info(sm->dev, "GPIO Control Hi %08x\n",
  170. smc501_readl(regs + SM501_GPIO63_32_CONTROL));
  171. dev_info(sm->dev, "DRAM Control %08x\n",
  172. smc501_readl(regs + SM501_DRAM_CONTROL));
  173. dev_info(sm->dev, "Arbitration Ctrl %08x\n",
  174. smc501_readl(regs + SM501_ARBTRTN_CONTROL));
  175. dev_info(sm->dev, "Misc Timing %08x\n",
  176. smc501_readl(regs + SM501_MISC_TIMING));
  177. }
  178. static void sm501_dump_gate(struct sm501_devdata *sm)
  179. {
  180. dev_info(sm->dev, "CurrentGate %08x\n",
  181. smc501_readl(sm->regs + SM501_CURRENT_GATE));
  182. dev_info(sm->dev, "CurrentClock %08x\n",
  183. smc501_readl(sm->regs + SM501_CURRENT_CLOCK));
  184. dev_info(sm->dev, "PowerModeControl %08x\n",
  185. smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL));
  186. }
  187. #else
  188. static inline void sm501_dump_gate(struct sm501_devdata *sm) { }
  189. static inline void sm501_dump_regs(struct sm501_devdata *sm) { }
  190. static inline void sm501_dump_clk(struct sm501_devdata *sm) { }
  191. #endif
  192. /* sm501_sync_regs
  193. *
  194. * ensure the
  195. */
  196. static void sm501_sync_regs(struct sm501_devdata *sm)
  197. {
  198. smc501_readl(sm->regs);
  199. }
  200. static inline void sm501_mdelay(struct sm501_devdata *sm, unsigned int delay)
  201. {
  202. /* during suspend/resume, we are currently not allowed to sleep,
  203. * so change to using mdelay() instead of msleep() if we
  204. * are in one of these paths */
  205. if (sm->in_suspend)
  206. mdelay(delay);
  207. else
  208. msleep(delay);
  209. }
  210. /* sm501_misc_control
  211. *
  212. * alters the miscellaneous control parameters
  213. */
  214. int sm501_misc_control(struct device *dev,
  215. unsigned long set, unsigned long clear)
  216. {
  217. struct sm501_devdata *sm = dev_get_drvdata(dev);
  218. unsigned long misc;
  219. unsigned long save;
  220. unsigned long to;
  221. spin_lock_irqsave(&sm->reg_lock, save);
  222. misc = smc501_readl(sm->regs + SM501_MISC_CONTROL);
  223. to = (misc & ~clear) | set;
  224. if (to != misc) {
  225. smc501_writel(to, sm->regs + SM501_MISC_CONTROL);
  226. sm501_sync_regs(sm);
  227. dev_dbg(sm->dev, "MISC_CONTROL %08lx\n", misc);
  228. }
  229. spin_unlock_irqrestore(&sm->reg_lock, save);
  230. return to;
  231. }
  232. EXPORT_SYMBOL_GPL(sm501_misc_control);
  233. /* sm501_modify_reg
  234. *
  235. * Modify a register in the SM501 which may be shared with other
  236. * drivers.
  237. */
  238. unsigned long sm501_modify_reg(struct device *dev,
  239. unsigned long reg,
  240. unsigned long set,
  241. unsigned long clear)
  242. {
  243. struct sm501_devdata *sm = dev_get_drvdata(dev);
  244. unsigned long data;
  245. unsigned long save;
  246. spin_lock_irqsave(&sm->reg_lock, save);
  247. data = smc501_readl(sm->regs + reg);
  248. data |= set;
  249. data &= ~clear;
  250. smc501_writel(data, sm->regs + reg);
  251. sm501_sync_regs(sm);
  252. spin_unlock_irqrestore(&sm->reg_lock, save);
  253. return data;
  254. }
  255. EXPORT_SYMBOL_GPL(sm501_modify_reg);
  256. /* sm501_unit_power
  257. *
  258. * alters the power active gate to set specific units on or off
  259. */
  260. int sm501_unit_power(struct device *dev, unsigned int unit, unsigned int to)
  261. {
  262. struct sm501_devdata *sm = dev_get_drvdata(dev);
  263. unsigned long mode;
  264. unsigned long gate;
  265. unsigned long clock;
  266. mutex_lock(&sm->clock_lock);
  267. mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
  268. gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
  269. clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
  270. mode &= 3; /* get current power mode */
  271. if (unit >= ARRAY_SIZE(sm->unit_power)) {
  272. dev_err(dev, "%s: bad unit %d\n", __func__, unit);
  273. goto already;
  274. }
  275. dev_dbg(sm->dev, "%s: unit %d, cur %d, to %d\n", __func__, unit,
  276. sm->unit_power[unit], to);
  277. if (to == 0 && sm->unit_power[unit] == 0) {
  278. dev_err(sm->dev, "unit %d is already shutdown\n", unit);
  279. goto already;
  280. }
  281. sm->unit_power[unit] += to ? 1 : -1;
  282. to = sm->unit_power[unit] ? 1 : 0;
  283. if (to) {
  284. if (gate & (1 << unit))
  285. goto already;
  286. gate |= (1 << unit);
  287. } else {
  288. if (!(gate & (1 << unit)))
  289. goto already;
  290. gate &= ~(1 << unit);
  291. }
  292. switch (mode) {
  293. case 1:
  294. smc501_writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
  295. smc501_writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
  296. mode = 0;
  297. break;
  298. case 2:
  299. case 0:
  300. smc501_writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
  301. smc501_writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
  302. mode = 1;
  303. break;
  304. default:
  305. gate = -1;
  306. goto already;
  307. }
  308. smc501_writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
  309. sm501_sync_regs(sm);
  310. dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
  311. gate, clock, mode);
  312. sm501_mdelay(sm, 16);
  313. already:
  314. mutex_unlock(&sm->clock_lock);
  315. return gate;
  316. }
  317. EXPORT_SYMBOL_GPL(sm501_unit_power);
  318. /* Perform a rounded division. */
  319. static long sm501fb_round_div(long num, long denom)
  320. {
  321. /* n / d + 1 / 2 = (2n + d) / 2d */
  322. return (2 * num + denom) / (2 * denom);
  323. }
  324. /* clock value structure. */
  325. struct sm501_clock {
  326. unsigned long mclk;
  327. int divider;
  328. int shift;
  329. unsigned int m, n, k;
  330. };
  331. /* sm501_calc_clock
  332. *
  333. * Calculates the nearest discrete clock frequency that
  334. * can be achieved with the specified input clock.
  335. * the maximum divisor is 3 or 5
  336. */
  337. static int sm501_calc_clock(unsigned long freq,
  338. struct sm501_clock *clock,
  339. int max_div,
  340. unsigned long mclk,
  341. long *best_diff)
  342. {
  343. int ret = 0;
  344. int divider;
  345. int shift;
  346. long diff;
  347. /* try dividers 1 and 3 for CRT and for panel,
  348. try divider 5 for panel only.*/
  349. for (divider = 1; divider <= max_div; divider += 2) {
  350. /* try all 8 shift values.*/
  351. for (shift = 0; shift < 8; shift++) {
  352. /* Calculate difference to requested clock */
  353. diff = sm501fb_round_div(mclk, divider << shift) - freq;
  354. if (diff < 0)
  355. diff = -diff;
  356. /* If it is less than the current, use it */
  357. if (diff < *best_diff) {
  358. *best_diff = diff;
  359. clock->mclk = mclk;
  360. clock->divider = divider;
  361. clock->shift = shift;
  362. ret = 1;
  363. }
  364. }
  365. }
  366. return ret;
  367. }
  368. /* sm501_calc_pll
  369. *
  370. * Calculates the nearest discrete clock frequency that can be
  371. * achieved using the programmable PLL.
  372. * the maximum divisor is 3 or 5
  373. */
  374. static unsigned long sm501_calc_pll(unsigned long freq,
  375. struct sm501_clock *clock,
  376. int max_div)
  377. {
  378. unsigned long mclk;
  379. unsigned int m, n, k;
  380. long best_diff = 999999999;
  381. /*
  382. * The SM502 datasheet doesn't specify the min/max values for M and N.
  383. * N = 1 at least doesn't work in practice.
  384. */
  385. for (m = 2; m <= 255; m++) {
  386. for (n = 2; n <= 127; n++) {
  387. for (k = 0; k <= 1; k++) {
  388. mclk = (24000000UL * m / n) >> k;
  389. if (sm501_calc_clock(freq, clock, max_div,
  390. mclk, &best_diff)) {
  391. clock->m = m;
  392. clock->n = n;
  393. clock->k = k;
  394. }
  395. }
  396. }
  397. }
  398. /* Return best clock. */
  399. return clock->mclk / (clock->divider << clock->shift);
  400. }
  401. /* sm501_select_clock
  402. *
  403. * Calculates the nearest discrete clock frequency that can be
  404. * achieved using the 288MHz and 336MHz PLLs.
  405. * the maximum divisor is 3 or 5
  406. */
  407. static unsigned long sm501_select_clock(unsigned long freq,
  408. struct sm501_clock *clock,
  409. int max_div)
  410. {
  411. unsigned long mclk;
  412. long best_diff = 999999999;
  413. /* Try 288MHz and 336MHz clocks. */
  414. for (mclk = 288000000; mclk <= 336000000; mclk += 48000000) {
  415. sm501_calc_clock(freq, clock, max_div, mclk, &best_diff);
  416. }
  417. /* Return best clock. */
  418. return clock->mclk / (clock->divider << clock->shift);
  419. }
  420. /* sm501_set_clock
  421. *
  422. * set one of the four clock sources to the closest available frequency to
  423. * the one specified
  424. */
  425. unsigned long sm501_set_clock(struct device *dev,
  426. int clksrc,
  427. unsigned long req_freq)
  428. {
  429. struct sm501_devdata *sm = dev_get_drvdata(dev);
  430. unsigned long mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
  431. unsigned long gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
  432. unsigned long clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
  433. unsigned char reg;
  434. unsigned int pll_reg = 0;
  435. unsigned long sm501_freq; /* the actual frequency achieved */
  436. struct sm501_clock to;
  437. /* find achivable discrete frequency and setup register value
  438. * accordingly, V2XCLK, MCLK and M1XCLK are the same P2XCLK
  439. * has an extra bit for the divider */
  440. switch (clksrc) {
  441. case SM501_CLOCK_P2XCLK:
  442. /* This clock is divided in half so to achieve the
  443. * requested frequency the value must be multiplied by
  444. * 2. This clock also has an additional pre divisor */
  445. if (sm->rev >= 0xC0) {
  446. /* SM502 -> use the programmable PLL */
  447. sm501_freq = (sm501_calc_pll(2 * req_freq,
  448. &to, 5) / 2);
  449. reg = to.shift & 0x07;/* bottom 3 bits are shift */
  450. if (to.divider == 3)
  451. reg |= 0x08; /* /3 divider required */
  452. else if (to.divider == 5)
  453. reg |= 0x10; /* /5 divider required */
  454. reg |= 0x40; /* select the programmable PLL */
  455. pll_reg = 0x20000 | (to.k << 15) | (to.n << 8) | to.m;
  456. } else {
  457. sm501_freq = (sm501_select_clock(2 * req_freq,
  458. &to, 5) / 2);
  459. reg = to.shift & 0x07;/* bottom 3 bits are shift */
  460. if (to.divider == 3)
  461. reg |= 0x08; /* /3 divider required */
  462. else if (to.divider == 5)
  463. reg |= 0x10; /* /5 divider required */
  464. if (to.mclk != 288000000)
  465. reg |= 0x20; /* which mclk pll is source */
  466. }
  467. break;
  468. case SM501_CLOCK_V2XCLK:
  469. /* This clock is divided in half so to achieve the
  470. * requested frequency the value must be multiplied by 2. */
  471. sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
  472. reg=to.shift & 0x07; /* bottom 3 bits are shift */
  473. if (to.divider == 3)
  474. reg |= 0x08; /* /3 divider required */
  475. if (to.mclk != 288000000)
  476. reg |= 0x10; /* which mclk pll is source */
  477. break;
  478. case SM501_CLOCK_MCLK:
  479. case SM501_CLOCK_M1XCLK:
  480. /* These clocks are the same and not further divided */
  481. sm501_freq = sm501_select_clock( req_freq, &to, 3);
  482. reg=to.shift & 0x07; /* bottom 3 bits are shift */
  483. if (to.divider == 3)
  484. reg |= 0x08; /* /3 divider required */
  485. if (to.mclk != 288000000)
  486. reg |= 0x10; /* which mclk pll is source */
  487. break;
  488. default:
  489. return 0; /* this is bad */
  490. }
  491. mutex_lock(&sm->clock_lock);
  492. mode = smc501_readl(sm->regs + SM501_POWER_MODE_CONTROL);
  493. gate = smc501_readl(sm->regs + SM501_CURRENT_GATE);
  494. clock = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
  495. clock = clock & ~(0xFF << clksrc);
  496. clock |= reg<<clksrc;
  497. mode &= 3; /* find current mode */
  498. switch (mode) {
  499. case 1:
  500. smc501_writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
  501. smc501_writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
  502. mode = 0;
  503. break;
  504. case 2:
  505. case 0:
  506. smc501_writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
  507. smc501_writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
  508. mode = 1;
  509. break;
  510. default:
  511. mutex_unlock(&sm->clock_lock);
  512. return -1;
  513. }
  514. smc501_writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
  515. if (pll_reg)
  516. smc501_writel(pll_reg,
  517. sm->regs + SM501_PROGRAMMABLE_PLL_CONTROL);
  518. sm501_sync_regs(sm);
  519. dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
  520. gate, clock, mode);
  521. sm501_mdelay(sm, 16);
  522. mutex_unlock(&sm->clock_lock);
  523. sm501_dump_clk(sm);
  524. return sm501_freq;
  525. }
  526. EXPORT_SYMBOL_GPL(sm501_set_clock);
  527. /* sm501_find_clock
  528. *
  529. * finds the closest available frequency for a given clock
  530. */
  531. unsigned long sm501_find_clock(struct device *dev,
  532. int clksrc,
  533. unsigned long req_freq)
  534. {
  535. struct sm501_devdata *sm = dev_get_drvdata(dev);
  536. unsigned long sm501_freq; /* the frequency achieveable by the 501 */
  537. struct sm501_clock to;
  538. switch (clksrc) {
  539. case SM501_CLOCK_P2XCLK:
  540. if (sm->rev >= 0xC0) {
  541. /* SM502 -> use the programmable PLL */
  542. sm501_freq = (sm501_calc_pll(2 * req_freq,
  543. &to, 5) / 2);
  544. } else {
  545. sm501_freq = (sm501_select_clock(2 * req_freq,
  546. &to, 5) / 2);
  547. }
  548. break;
  549. case SM501_CLOCK_V2XCLK:
  550. sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
  551. break;
  552. case SM501_CLOCK_MCLK:
  553. case SM501_CLOCK_M1XCLK:
  554. sm501_freq = sm501_select_clock(req_freq, &to, 3);
  555. break;
  556. default:
  557. sm501_freq = 0; /* error */
  558. }
  559. return sm501_freq;
  560. }
  561. EXPORT_SYMBOL_GPL(sm501_find_clock);
  562. static struct sm501_device *to_sm_device(struct platform_device *pdev)
  563. {
  564. return container_of(pdev, struct sm501_device, pdev);
  565. }
  566. /* sm501_device_release
  567. *
  568. * A release function for the platform devices we create to allow us to
  569. * free any items we allocated
  570. */
  571. static void sm501_device_release(struct device *dev)
  572. {
  573. kfree(to_sm_device(to_platform_device(dev)));
  574. }
  575. /* sm501_create_subdev
  576. *
  577. * Create a skeleton platform device with resources for passing to a
  578. * sub-driver
  579. */
  580. static struct platform_device *
  581. sm501_create_subdev(struct sm501_devdata *sm, char *name,
  582. unsigned int res_count, unsigned int platform_data_size)
  583. {
  584. struct sm501_device *smdev;
  585. smdev = kzalloc(sizeof(struct sm501_device) +
  586. (sizeof(struct resource) * res_count) +
  587. platform_data_size, GFP_KERNEL);
  588. if (!smdev)
  589. return NULL;
  590. smdev->pdev.dev.release = sm501_device_release;
  591. smdev->pdev.name = name;
  592. smdev->pdev.id = sm->pdev_id;
  593. smdev->pdev.dev.parent = sm->dev;
  594. if (res_count) {
  595. smdev->pdev.resource = (struct resource *)(smdev+1);
  596. smdev->pdev.num_resources = res_count;
  597. }
  598. if (platform_data_size)
  599. smdev->pdev.dev.platform_data = (void *)(smdev+1);
  600. return &smdev->pdev;
  601. }
  602. /* sm501_register_device
  603. *
  604. * Register a platform device created with sm501_create_subdev()
  605. */
  606. static int sm501_register_device(struct sm501_devdata *sm,
  607. struct platform_device *pdev)
  608. {
  609. struct sm501_device *smdev = to_sm_device(pdev);
  610. int ptr;
  611. int ret;
  612. for (ptr = 0; ptr < pdev->num_resources; ptr++) {
  613. printk(KERN_DEBUG "%s[%d] %pR\n",
  614. pdev->name, ptr, &pdev->resource[ptr]);
  615. }
  616. ret = platform_device_register(pdev);
  617. if (ret >= 0) {
  618. dev_dbg(sm->dev, "registered %s\n", pdev->name);
  619. list_add_tail(&smdev->list, &sm->devices);
  620. } else
  621. dev_err(sm->dev, "error registering %s (%d)\n",
  622. pdev->name, ret);
  623. return ret;
  624. }
  625. /* sm501_create_subio
  626. *
  627. * Fill in an IO resource for a sub device
  628. */
  629. static void sm501_create_subio(struct sm501_devdata *sm,
  630. struct resource *res,
  631. resource_size_t offs,
  632. resource_size_t size)
  633. {
  634. res->flags = IORESOURCE_MEM;
  635. res->parent = sm->io_res;
  636. res->start = sm->io_res->start + offs;
  637. res->end = res->start + size - 1;
  638. }
  639. /* sm501_create_mem
  640. *
  641. * Fill in an MEM resource for a sub device
  642. */
  643. static void sm501_create_mem(struct sm501_devdata *sm,
  644. struct resource *res,
  645. resource_size_t *offs,
  646. resource_size_t size)
  647. {
  648. *offs -= size; /* adjust memory size */
  649. res->flags = IORESOURCE_MEM;
  650. res->parent = sm->mem_res;
  651. res->start = sm->mem_res->start + *offs;
  652. res->end = res->start + size - 1;
  653. }
  654. /* sm501_create_irq
  655. *
  656. * Fill in an IRQ resource for a sub device
  657. */
  658. static void sm501_create_irq(struct sm501_devdata *sm,
  659. struct resource *res)
  660. {
  661. res->flags = IORESOURCE_IRQ;
  662. res->parent = NULL;
  663. res->start = res->end = sm->irq;
  664. }
  665. static int sm501_register_usbhost(struct sm501_devdata *sm,
  666. resource_size_t *mem_avail)
  667. {
  668. struct platform_device *pdev;
  669. pdev = sm501_create_subdev(sm, "sm501-usb", 3, 0);
  670. if (!pdev)
  671. return -ENOMEM;
  672. sm501_create_subio(sm, &pdev->resource[0], 0x40000, 0x20000);
  673. sm501_create_mem(sm, &pdev->resource[1], mem_avail, 256*1024);
  674. sm501_create_irq(sm, &pdev->resource[2]);
  675. return sm501_register_device(sm, pdev);
  676. }
  677. static void sm501_setup_uart_data(struct sm501_devdata *sm,
  678. struct plat_serial8250_port *uart_data,
  679. unsigned int offset)
  680. {
  681. uart_data->membase = sm->regs + offset;
  682. uart_data->mapbase = sm->io_res->start + offset;
  683. uart_data->iotype = UPIO_MEM;
  684. uart_data->irq = sm->irq;
  685. uart_data->flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
  686. uart_data->regshift = 2;
  687. uart_data->uartclk = (9600 * 16);
  688. }
  689. static int sm501_register_uart(struct sm501_devdata *sm, int devices)
  690. {
  691. struct platform_device *pdev;
  692. struct plat_serial8250_port *uart_data;
  693. pdev = sm501_create_subdev(sm, "serial8250", 0,
  694. sizeof(struct plat_serial8250_port) * 3);
  695. if (!pdev)
  696. return -ENOMEM;
  697. uart_data = pdev->dev.platform_data;
  698. if (devices & SM501_USE_UART0) {
  699. sm501_setup_uart_data(sm, uart_data++, 0x30000);
  700. sm501_unit_power(sm->dev, SM501_GATE_UART0, 1);
  701. sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 12, 0);
  702. sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x01e0, 0);
  703. }
  704. if (devices & SM501_USE_UART1) {
  705. sm501_setup_uart_data(sm, uart_data++, 0x30020);
  706. sm501_unit_power(sm->dev, SM501_GATE_UART1, 1);
  707. sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 13, 0);
  708. sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x1e00, 0);
  709. }
  710. pdev->id = PLAT8250_DEV_SM501;
  711. return sm501_register_device(sm, pdev);
  712. }
  713. static int sm501_register_display(struct sm501_devdata *sm,
  714. resource_size_t *mem_avail)
  715. {
  716. struct platform_device *pdev;
  717. pdev = sm501_create_subdev(sm, "sm501-fb", 4, 0);
  718. if (!pdev)
  719. return -ENOMEM;
  720. sm501_create_subio(sm, &pdev->resource[0], 0x80000, 0x10000);
  721. sm501_create_subio(sm, &pdev->resource[1], 0x100000, 0x50000);
  722. sm501_create_mem(sm, &pdev->resource[2], mem_avail, *mem_avail);
  723. sm501_create_irq(sm, &pdev->resource[3]);
  724. return sm501_register_device(sm, pdev);
  725. }
  726. #ifdef CONFIG_MFD_SM501_GPIO
  727. static inline struct sm501_gpio_chip *to_sm501_gpio(struct gpio_chip *gc)
  728. {
  729. return container_of(gc, struct sm501_gpio_chip, gpio);
  730. }
  731. static inline struct sm501_devdata *sm501_gpio_to_dev(struct sm501_gpio *gpio)
  732. {
  733. return container_of(gpio, struct sm501_devdata, gpio);
  734. }
  735. static int sm501_gpio_get(struct gpio_chip *chip, unsigned offset)
  736. {
  737. struct sm501_gpio_chip *smgpio = to_sm501_gpio(chip);
  738. unsigned long result;
  739. result = smc501_readl(smgpio->regbase + SM501_GPIO_DATA_LOW);
  740. result >>= offset;
  741. return result & 1UL;
  742. }
  743. static void sm501_gpio_ensure_gpio(struct sm501_gpio_chip *smchip,
  744. unsigned long bit)
  745. {
  746. unsigned long ctrl;
  747. /* check and modify if this pin is not set as gpio. */
  748. if (smc501_readl(smchip->control) & bit) {
  749. dev_info(sm501_gpio_to_dev(smchip->ourgpio)->dev,
  750. "changing mode of gpio, bit %08lx\n", bit);
  751. ctrl = smc501_readl(smchip->control);
  752. ctrl &= ~bit;
  753. smc501_writel(ctrl, smchip->control);
  754. sm501_sync_regs(sm501_gpio_to_dev(smchip->ourgpio));
  755. }
  756. }
  757. static void sm501_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  758. {
  759. struct sm501_gpio_chip *smchip = to_sm501_gpio(chip);
  760. struct sm501_gpio *smgpio = smchip->ourgpio;
  761. unsigned long bit = 1 << offset;
  762. void __iomem *regs = smchip->regbase;
  763. unsigned long save;
  764. unsigned long val;
  765. dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d)\n",
  766. __func__, chip, offset);
  767. spin_lock_irqsave(&smgpio->lock, save);
  768. val = smc501_readl(regs + SM501_GPIO_DATA_LOW) & ~bit;
  769. if (value)
  770. val |= bit;
  771. smc501_writel(val, regs);
  772. sm501_sync_regs(sm501_gpio_to_dev(smgpio));
  773. sm501_gpio_ensure_gpio(smchip, bit);
  774. spin_unlock_irqrestore(&smgpio->lock, save);
  775. }
  776. static int sm501_gpio_input(struct gpio_chip *chip, unsigned offset)
  777. {
  778. struct sm501_gpio_chip *smchip = to_sm501_gpio(chip);
  779. struct sm501_gpio *smgpio = smchip->ourgpio;
  780. void __iomem *regs = smchip->regbase;
  781. unsigned long bit = 1 << offset;
  782. unsigned long save;
  783. unsigned long ddr;
  784. dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d)\n",
  785. __func__, chip, offset);
  786. spin_lock_irqsave(&smgpio->lock, save);
  787. ddr = smc501_readl(regs + SM501_GPIO_DDR_LOW);
  788. smc501_writel(ddr & ~bit, regs + SM501_GPIO_DDR_LOW);
  789. sm501_sync_regs(sm501_gpio_to_dev(smgpio));
  790. sm501_gpio_ensure_gpio(smchip, bit);
  791. spin_unlock_irqrestore(&smgpio->lock, save);
  792. return 0;
  793. }
  794. static int sm501_gpio_output(struct gpio_chip *chip,
  795. unsigned offset, int value)
  796. {
  797. struct sm501_gpio_chip *smchip = to_sm501_gpio(chip);
  798. struct sm501_gpio *smgpio = smchip->ourgpio;
  799. unsigned long bit = 1 << offset;
  800. void __iomem *regs = smchip->regbase;
  801. unsigned long save;
  802. unsigned long val;
  803. unsigned long ddr;
  804. dev_dbg(sm501_gpio_to_dev(smgpio)->dev, "%s(%p,%d,%d)\n",
  805. __func__, chip, offset, value);
  806. spin_lock_irqsave(&smgpio->lock, save);
  807. val = smc501_readl(regs + SM501_GPIO_DATA_LOW);
  808. if (value)
  809. val |= bit;
  810. else
  811. val &= ~bit;
  812. smc501_writel(val, regs);
  813. ddr = smc501_readl(regs + SM501_GPIO_DDR_LOW);
  814. smc501_writel(ddr | bit, regs + SM501_GPIO_DDR_LOW);
  815. sm501_sync_regs(sm501_gpio_to_dev(smgpio));
  816. smc501_writel(val, regs + SM501_GPIO_DATA_LOW);
  817. sm501_sync_regs(sm501_gpio_to_dev(smgpio));
  818. spin_unlock_irqrestore(&smgpio->lock, save);
  819. return 0;
  820. }
  821. static struct gpio_chip gpio_chip_template = {
  822. .ngpio = 32,
  823. .direction_input = sm501_gpio_input,
  824. .direction_output = sm501_gpio_output,
  825. .set = sm501_gpio_set,
  826. .get = sm501_gpio_get,
  827. };
  828. static int __devinit sm501_gpio_register_chip(struct sm501_devdata *sm,
  829. struct sm501_gpio *gpio,
  830. struct sm501_gpio_chip *chip)
  831. {
  832. struct sm501_platdata *pdata = sm->platdata;
  833. struct gpio_chip *gchip = &chip->gpio;
  834. int base = pdata->gpio_base;
  835. chip->gpio = gpio_chip_template;
  836. if (chip == &gpio->high) {
  837. if (base > 0)
  838. base += 32;
  839. chip->regbase = gpio->regs + SM501_GPIO_DATA_HIGH;
  840. chip->control = sm->regs + SM501_GPIO63_32_CONTROL;
  841. gchip->label = "SM501-HIGH";
  842. } else {
  843. chip->regbase = gpio->regs + SM501_GPIO_DATA_LOW;
  844. chip->control = sm->regs + SM501_GPIO31_0_CONTROL;
  845. gchip->label = "SM501-LOW";
  846. }
  847. gchip->base = base;
  848. chip->ourgpio = gpio;
  849. return gpiochip_add(gchip);
  850. }
  851. static int __devinit sm501_register_gpio(struct sm501_devdata *sm)
  852. {
  853. struct sm501_gpio *gpio = &sm->gpio;
  854. resource_size_t iobase = sm->io_res->start + SM501_GPIO;
  855. int ret;
  856. int tmp;
  857. dev_dbg(sm->dev, "registering gpio block %08llx\n",
  858. (unsigned long long)iobase);
  859. spin_lock_init(&gpio->lock);
  860. gpio->regs_res = request_mem_region(iobase, 0x20, "sm501-gpio");
  861. if (gpio->regs_res == NULL) {
  862. dev_err(sm->dev, "gpio: failed to request region\n");
  863. return -ENXIO;
  864. }
  865. gpio->regs = ioremap(iobase, 0x20);
  866. if (gpio->regs == NULL) {
  867. dev_err(sm->dev, "gpio: failed to remap registers\n");
  868. ret = -ENXIO;
  869. goto err_claimed;
  870. }
  871. /* Register both our chips. */
  872. ret = sm501_gpio_register_chip(sm, gpio, &gpio->low);
  873. if (ret) {
  874. dev_err(sm->dev, "failed to add low chip\n");
  875. goto err_mapped;
  876. }
  877. ret = sm501_gpio_register_chip(sm, gpio, &gpio->high);
  878. if (ret) {
  879. dev_err(sm->dev, "failed to add high chip\n");
  880. goto err_low_chip;
  881. }
  882. gpio->registered = 1;
  883. return 0;
  884. err_low_chip:
  885. tmp = gpiochip_remove(&gpio->low.gpio);
  886. if (tmp) {
  887. dev_err(sm->dev, "cannot remove low chip, cannot tidy up\n");
  888. return ret;
  889. }
  890. err_mapped:
  891. iounmap(gpio->regs);
  892. err_claimed:
  893. release_resource(gpio->regs_res);
  894. kfree(gpio->regs_res);
  895. return ret;
  896. }
  897. static void sm501_gpio_remove(struct sm501_devdata *sm)
  898. {
  899. struct sm501_gpio *gpio = &sm->gpio;
  900. int ret;
  901. if (!sm->gpio.registered)
  902. return;
  903. ret = gpiochip_remove(&gpio->low.gpio);
  904. if (ret)
  905. dev_err(sm->dev, "cannot remove low chip, cannot tidy up\n");
  906. ret = gpiochip_remove(&gpio->high.gpio);
  907. if (ret)
  908. dev_err(sm->dev, "cannot remove high chip, cannot tidy up\n");
  909. iounmap(gpio->regs);
  910. release_resource(gpio->regs_res);
  911. kfree(gpio->regs_res);
  912. }
  913. static inline int sm501_gpio_pin2nr(struct sm501_devdata *sm, unsigned int pin)
  914. {
  915. struct sm501_gpio *gpio = &sm->gpio;
  916. int base = (pin < 32) ? gpio->low.gpio.base : gpio->high.gpio.base;
  917. return (pin % 32) + base;
  918. }
  919. static inline int sm501_gpio_isregistered(struct sm501_devdata *sm)
  920. {
  921. return sm->gpio.registered;
  922. }
  923. #else
  924. static inline int sm501_register_gpio(struct sm501_devdata *sm)
  925. {
  926. return 0;
  927. }
  928. static inline void sm501_gpio_remove(struct sm501_devdata *sm)
  929. {
  930. }
  931. static inline int sm501_gpio_pin2nr(struct sm501_devdata *sm, unsigned int pin)
  932. {
  933. return -1;
  934. }
  935. static inline int sm501_gpio_isregistered(struct sm501_devdata *sm)
  936. {
  937. return 0;
  938. }
  939. #endif
  940. static int sm501_register_gpio_i2c_instance(struct sm501_devdata *sm,
  941. struct sm501_platdata_gpio_i2c *iic)
  942. {
  943. struct i2c_gpio_platform_data *icd;
  944. struct platform_device *pdev;
  945. pdev = sm501_create_subdev(sm, "i2c-gpio", 0,
  946. sizeof(struct i2c_gpio_platform_data));
  947. if (!pdev)
  948. return -ENOMEM;
  949. icd = pdev->dev.platform_data;
  950. /* We keep the pin_sda and pin_scl fields relative in case the
  951. * same platform data is passed to >1 SM501.
  952. */
  953. icd->sda_pin = sm501_gpio_pin2nr(sm, iic->pin_sda);
  954. icd->scl_pin = sm501_gpio_pin2nr(sm, iic->pin_scl);
  955. icd->timeout = iic->timeout;
  956. icd->udelay = iic->udelay;
  957. /* note, we can't use either of the pin numbers, as the i2c-gpio
  958. * driver uses the platform.id field to generate the bus number
  959. * to register with the i2c core; The i2c core doesn't have enough
  960. * entries to deal with anything we currently use.
  961. */
  962. pdev->id = iic->bus_num;
  963. dev_info(sm->dev, "registering i2c-%d: sda=%d (%d), scl=%d (%d)\n",
  964. iic->bus_num,
  965. icd->sda_pin, iic->pin_sda, icd->scl_pin, iic->pin_scl);
  966. return sm501_register_device(sm, pdev);
  967. }
  968. static int sm501_register_gpio_i2c(struct sm501_devdata *sm,
  969. struct sm501_platdata *pdata)
  970. {
  971. struct sm501_platdata_gpio_i2c *iic = pdata->gpio_i2c;
  972. int index;
  973. int ret;
  974. for (index = 0; index < pdata->gpio_i2c_nr; index++, iic++) {
  975. ret = sm501_register_gpio_i2c_instance(sm, iic);
  976. if (ret < 0)
  977. return ret;
  978. }
  979. return 0;
  980. }
  981. /* sm501_dbg_regs
  982. *
  983. * Debug attribute to attach to parent device to show core registers
  984. */
  985. static ssize_t sm501_dbg_regs(struct device *dev,
  986. struct device_attribute *attr, char *buff)
  987. {
  988. struct sm501_devdata *sm = dev_get_drvdata(dev) ;
  989. unsigned int reg;
  990. char *ptr = buff;
  991. int ret;
  992. for (reg = 0x00; reg < 0x70; reg += 4) {
  993. ret = sprintf(ptr, "%08x = %08x\n",
  994. reg, smc501_readl(sm->regs + reg));
  995. ptr += ret;
  996. }
  997. return ptr - buff;
  998. }
  999. static DEVICE_ATTR(dbg_regs, 0666, sm501_dbg_regs, NULL);
  1000. /* sm501_init_reg
  1001. *
  1002. * Helper function for the init code to setup a register
  1003. *
  1004. * clear the bits which are set in r->mask, and then set
  1005. * the bits set in r->set.
  1006. */
  1007. static inline void sm501_init_reg(struct sm501_devdata *sm,
  1008. unsigned long reg,
  1009. struct sm501_reg_init *r)
  1010. {
  1011. unsigned long tmp;
  1012. tmp = smc501_readl(sm->regs + reg);
  1013. tmp &= ~r->mask;
  1014. tmp |= r->set;
  1015. smc501_writel(tmp, sm->regs + reg);
  1016. }
  1017. /* sm501_init_regs
  1018. *
  1019. * Setup core register values
  1020. */
  1021. static void sm501_init_regs(struct sm501_devdata *sm,
  1022. struct sm501_initdata *init)
  1023. {
  1024. sm501_misc_control(sm->dev,
  1025. init->misc_control.set,
  1026. init->misc_control.mask);
  1027. sm501_init_reg(sm, SM501_MISC_TIMING, &init->misc_timing);
  1028. sm501_init_reg(sm, SM501_GPIO31_0_CONTROL, &init->gpio_low);
  1029. sm501_init_reg(sm, SM501_GPIO63_32_CONTROL, &init->gpio_high);
  1030. if (init->m1xclk) {
  1031. dev_info(sm->dev, "setting M1XCLK to %ld\n", init->m1xclk);
  1032. sm501_set_clock(sm->dev, SM501_CLOCK_M1XCLK, init->m1xclk);
  1033. }
  1034. if (init->mclk) {
  1035. dev_info(sm->dev, "setting MCLK to %ld\n", init->mclk);
  1036. sm501_set_clock(sm->dev, SM501_CLOCK_MCLK, init->mclk);
  1037. }
  1038. }
  1039. /* Check the PLL sources for the M1CLK and M1XCLK
  1040. *
  1041. * If the M1CLK and M1XCLKs are not sourced from the same PLL, then
  1042. * there is a risk (see errata AB-5) that the SM501 will cease proper
  1043. * function. If this happens, then it is likely the SM501 will
  1044. * hang the system.
  1045. */
  1046. static int sm501_check_clocks(struct sm501_devdata *sm)
  1047. {
  1048. unsigned long pwrmode = smc501_readl(sm->regs + SM501_CURRENT_CLOCK);
  1049. unsigned long msrc = (pwrmode & SM501_POWERMODE_M_SRC);
  1050. unsigned long m1src = (pwrmode & SM501_POWERMODE_M1_SRC);
  1051. return ((msrc == 0 && m1src != 0) || (msrc != 0 && m1src == 0));
  1052. }
  1053. static unsigned int sm501_mem_local[] = {
  1054. [0] = 4*1024*1024,
  1055. [1] = 8*1024*1024,
  1056. [2] = 16*1024*1024,
  1057. [3] = 32*1024*1024,
  1058. [4] = 64*1024*1024,
  1059. [5] = 2*1024*1024,
  1060. };
  1061. /* sm501_init_dev
  1062. *
  1063. * Common init code for an SM501
  1064. */
  1065. static int __devinit sm501_init_dev(struct sm501_devdata *sm)
  1066. {
  1067. struct sm501_initdata *idata;
  1068. struct sm501_platdata *pdata;
  1069. resource_size_t mem_avail;
  1070. unsigned long dramctrl;
  1071. unsigned long devid;
  1072. int ret;
  1073. mutex_init(&sm->clock_lock);
  1074. spin_lock_init(&sm->reg_lock);
  1075. INIT_LIST_HEAD(&sm->devices);
  1076. devid = smc501_readl(sm->regs + SM501_DEVICEID);
  1077. if ((devid & SM501_DEVICEID_IDMASK) != SM501_DEVICEID_SM501) {
  1078. dev_err(sm->dev, "incorrect device id %08lx\n", devid);
  1079. return -EINVAL;
  1080. }
  1081. /* disable irqs */
  1082. smc501_writel(0, sm->regs + SM501_IRQ_MASK);
  1083. dramctrl = smc501_readl(sm->regs + SM501_DRAM_CONTROL);
  1084. mem_avail = sm501_mem_local[(dramctrl >> 13) & 0x7];
  1085. dev_info(sm->dev, "SM501 At %p: Version %08lx, %ld Mb, IRQ %d\n",
  1086. sm->regs, devid, (unsigned long)mem_avail >> 20, sm->irq);
  1087. sm->rev = devid & SM501_DEVICEID_REVMASK;
  1088. sm501_dump_gate(sm);
  1089. ret = device_create_file(sm->dev, &dev_attr_dbg_regs);
  1090. if (ret)
  1091. dev_err(sm->dev, "failed to create debug regs file\n");
  1092. sm501_dump_clk(sm);
  1093. /* check to see if we have some device initialisation */
  1094. pdata = sm->platdata;
  1095. idata = pdata ? pdata->init : NULL;
  1096. if (idata) {
  1097. sm501_init_regs(sm, idata);
  1098. if (idata->devices & SM501_USE_USB_HOST)
  1099. sm501_register_usbhost(sm, &mem_avail);
  1100. if (idata->devices & (SM501_USE_UART0 | SM501_USE_UART1))
  1101. sm501_register_uart(sm, idata->devices);
  1102. if (idata->devices & SM501_USE_GPIO)
  1103. sm501_register_gpio(sm);
  1104. }
  1105. if (pdata && pdata->gpio_i2c != NULL && pdata->gpio_i2c_nr > 0) {
  1106. if (!sm501_gpio_isregistered(sm))
  1107. dev_err(sm->dev, "no gpio available for i2c gpio.\n");
  1108. else
  1109. sm501_register_gpio_i2c(sm, pdata);
  1110. }
  1111. ret = sm501_check_clocks(sm);
  1112. if (ret) {
  1113. dev_err(sm->dev, "M1X and M clocks sourced from different "
  1114. "PLLs\n");
  1115. return -EINVAL;
  1116. }
  1117. /* always create a framebuffer */
  1118. sm501_register_display(sm, &mem_avail);
  1119. return 0;
  1120. }
  1121. static int __devinit sm501_plat_probe(struct platform_device *dev)
  1122. {
  1123. struct sm501_devdata *sm;
  1124. int ret;
  1125. sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
  1126. if (sm == NULL) {
  1127. dev_err(&dev->dev, "no memory for device data\n");
  1128. ret = -ENOMEM;
  1129. goto err1;
  1130. }
  1131. sm->dev = &dev->dev;
  1132. sm->pdev_id = dev->id;
  1133. sm->platdata = dev->dev.platform_data;
  1134. ret = platform_get_irq(dev, 0);
  1135. if (ret < 0) {
  1136. dev_err(&dev->dev, "failed to get irq resource\n");
  1137. goto err_res;
  1138. }
  1139. sm->irq = ret;
  1140. sm->io_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  1141. sm->mem_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  1142. if (sm->io_res == NULL || sm->mem_res == NULL) {
  1143. dev_err(&dev->dev, "failed to get IO resource\n");
  1144. ret = -ENOENT;
  1145. goto err_res;
  1146. }
  1147. sm->regs_claim = request_mem_region(sm->io_res->start,
  1148. 0x100, "sm501");
  1149. if (sm->regs_claim == NULL) {
  1150. dev_err(&dev->dev, "cannot claim registers\n");
  1151. ret = -EBUSY;
  1152. goto err_res;
  1153. }
  1154. platform_set_drvdata(dev, sm);
  1155. sm->regs = ioremap(sm->io_res->start, resource_size(sm->io_res));
  1156. if (sm->regs == NULL) {
  1157. dev_err(&dev->dev, "cannot remap registers\n");
  1158. ret = -EIO;
  1159. goto err_claim;
  1160. }
  1161. return sm501_init_dev(sm);
  1162. err_claim:
  1163. release_resource(sm->regs_claim);
  1164. kfree(sm->regs_claim);
  1165. err_res:
  1166. kfree(sm);
  1167. err1:
  1168. return ret;
  1169. }
  1170. #ifdef CONFIG_PM
  1171. /* power management support */
  1172. static void sm501_set_power(struct sm501_devdata *sm, int on)
  1173. {
  1174. struct sm501_platdata *pd = sm->platdata;
  1175. if (pd == NULL)
  1176. return;
  1177. if (pd->get_power) {
  1178. if (pd->get_power(sm->dev) == on) {
  1179. dev_dbg(sm->dev, "is already %d\n", on);
  1180. return;
  1181. }
  1182. }
  1183. if (pd->set_power) {
  1184. dev_dbg(sm->dev, "setting power to %d\n", on);
  1185. pd->set_power(sm->dev, on);
  1186. sm501_mdelay(sm, 10);
  1187. }
  1188. }
  1189. static int sm501_plat_suspend(struct platform_device *pdev, pm_message_t state)
  1190. {
  1191. struct sm501_devdata *sm = platform_get_drvdata(pdev);
  1192. sm->in_suspend = 1;
  1193. sm->pm_misc = smc501_readl(sm->regs + SM501_MISC_CONTROL);
  1194. sm501_dump_regs(sm);
  1195. if (sm->platdata) {
  1196. if (sm->platdata->flags & SM501_FLAG_SUSPEND_OFF)
  1197. sm501_set_power(sm, 0);
  1198. }
  1199. return 0;
  1200. }
  1201. static int sm501_plat_resume(struct platform_device *pdev)
  1202. {
  1203. struct sm501_devdata *sm = platform_get_drvdata(pdev);
  1204. sm501_set_power(sm, 1);
  1205. sm501_dump_regs(sm);
  1206. sm501_dump_gate(sm);
  1207. sm501_dump_clk(sm);
  1208. /* check to see if we are in the same state as when suspended */
  1209. if (smc501_readl(sm->regs + SM501_MISC_CONTROL) != sm->pm_misc) {
  1210. dev_info(sm->dev, "SM501_MISC_CONTROL changed over sleep\n");
  1211. smc501_writel(sm->pm_misc, sm->regs + SM501_MISC_CONTROL);
  1212. /* our suspend causes the controller state to change,
  1213. * either by something attempting setup, power loss,
  1214. * or an external reset event on power change */
  1215. if (sm->platdata && sm->platdata->init) {
  1216. sm501_init_regs(sm, sm->platdata->init);
  1217. }
  1218. }
  1219. /* dump our state from resume */
  1220. sm501_dump_regs(sm);
  1221. sm501_dump_clk(sm);
  1222. sm->in_suspend = 0;
  1223. return 0;
  1224. }
  1225. #else
  1226. #define sm501_plat_suspend NULL
  1227. #define sm501_plat_resume NULL
  1228. #endif
  1229. /* Initialisation data for PCI devices */
  1230. static struct sm501_initdata sm501_pci_initdata = {
  1231. .gpio_high = {
  1232. .set = 0x3F000000, /* 24bit panel */
  1233. .mask = 0x0,
  1234. },
  1235. .misc_timing = {
  1236. .set = 0x010100, /* SDRAM timing */
  1237. .mask = 0x1F1F00,
  1238. },
  1239. .misc_control = {
  1240. .set = SM501_MISC_PNL_24BIT,
  1241. .mask = 0,
  1242. },
  1243. .devices = SM501_USE_ALL,
  1244. /* Errata AB-3 says that 72MHz is the fastest available
  1245. * for 33MHZ PCI with proper bus-mastering operation */
  1246. .mclk = 72 * MHZ,
  1247. .m1xclk = 144 * MHZ,
  1248. };
  1249. static struct sm501_platdata_fbsub sm501_pdata_fbsub = {
  1250. .flags = (SM501FB_FLAG_USE_INIT_MODE |
  1251. SM501FB_FLAG_USE_HWCURSOR |
  1252. SM501FB_FLAG_USE_HWACCEL |
  1253. SM501FB_FLAG_DISABLE_AT_EXIT),
  1254. };
  1255. static struct sm501_platdata_fb sm501_fb_pdata = {
  1256. .fb_route = SM501_FB_OWN,
  1257. .fb_crt = &sm501_pdata_fbsub,
  1258. .fb_pnl = &sm501_pdata_fbsub,
  1259. };
  1260. static struct sm501_platdata sm501_pci_platdata = {
  1261. .init = &sm501_pci_initdata,
  1262. .fb = &sm501_fb_pdata,
  1263. .gpio_base = -1,
  1264. };
  1265. static int __devinit sm501_pci_probe(struct pci_dev *dev,
  1266. const struct pci_device_id *id)
  1267. {
  1268. struct sm501_devdata *sm;
  1269. int err;
  1270. sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
  1271. if (sm == NULL) {
  1272. dev_err(&dev->dev, "no memory for device data\n");
  1273. err = -ENOMEM;
  1274. goto err1;
  1275. }
  1276. /* set a default set of platform data */
  1277. dev->dev.platform_data = sm->platdata = &sm501_pci_platdata;
  1278. /* set a hopefully unique id for our child platform devices */
  1279. sm->pdev_id = 32 + dev->devfn;
  1280. pci_set_drvdata(dev, sm);
  1281. err = pci_enable_device(dev);
  1282. if (err) {
  1283. dev_err(&dev->dev, "cannot enable device\n");
  1284. goto err2;
  1285. }
  1286. sm->dev = &dev->dev;
  1287. sm->irq = dev->irq;
  1288. #ifdef __BIG_ENDIAN
  1289. /* if the system is big-endian, we most probably have a
  1290. * translation in the IO layer making the PCI bus little endian
  1291. * so make the framebuffer swapped pixels */
  1292. sm501_fb_pdata.flags |= SM501_FBPD_SWAP_FB_ENDIAN;
  1293. #endif
  1294. /* check our resources */
  1295. if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM)) {
  1296. dev_err(&dev->dev, "region #0 is not memory?\n");
  1297. err = -EINVAL;
  1298. goto err3;
  1299. }
  1300. if (!(pci_resource_flags(dev, 1) & IORESOURCE_MEM)) {
  1301. dev_err(&dev->dev, "region #1 is not memory?\n");
  1302. err = -EINVAL;
  1303. goto err3;
  1304. }
  1305. /* make our resources ready for sharing */
  1306. sm->io_res = &dev->resource[1];
  1307. sm->mem_res = &dev->resource[0];
  1308. sm->regs_claim = request_mem_region(sm->io_res->start,
  1309. 0x100, "sm501");
  1310. if (sm->regs_claim == NULL) {
  1311. dev_err(&dev->dev, "cannot claim registers\n");
  1312. err= -EBUSY;
  1313. goto err3;
  1314. }
  1315. sm->regs = pci_ioremap_bar(dev, 1);
  1316. if (sm->regs == NULL) {
  1317. dev_err(&dev->dev, "cannot remap registers\n");
  1318. err = -EIO;
  1319. goto err4;
  1320. }
  1321. sm501_init_dev(sm);
  1322. return 0;
  1323. err4:
  1324. release_resource(sm->regs_claim);
  1325. kfree(sm->regs_claim);
  1326. err3:
  1327. pci_disable_device(dev);
  1328. err2:
  1329. pci_set_drvdata(dev, NULL);
  1330. kfree(sm);
  1331. err1:
  1332. return err;
  1333. }
  1334. static void sm501_remove_sub(struct sm501_devdata *sm,
  1335. struct sm501_device *smdev)
  1336. {
  1337. list_del(&smdev->list);
  1338. platform_device_unregister(&smdev->pdev);
  1339. }
  1340. static void sm501_dev_remove(struct sm501_devdata *sm)
  1341. {
  1342. struct sm501_device *smdev, *tmp;
  1343. list_for_each_entry_safe(smdev, tmp, &sm->devices, list)
  1344. sm501_remove_sub(sm, smdev);
  1345. device_remove_file(sm->dev, &dev_attr_dbg_regs);
  1346. sm501_gpio_remove(sm);
  1347. }
  1348. static void __devexit sm501_pci_remove(struct pci_dev *dev)
  1349. {
  1350. struct sm501_devdata *sm = pci_get_drvdata(dev);
  1351. sm501_dev_remove(sm);
  1352. iounmap(sm->regs);
  1353. release_resource(sm->regs_claim);
  1354. kfree(sm->regs_claim);
  1355. pci_set_drvdata(dev, NULL);
  1356. pci_disable_device(dev);
  1357. }
  1358. static int sm501_plat_remove(struct platform_device *dev)
  1359. {
  1360. struct sm501_devdata *sm = platform_get_drvdata(dev);
  1361. sm501_dev_remove(sm);
  1362. iounmap(sm->regs);
  1363. release_resource(sm->regs_claim);
  1364. kfree(sm->regs_claim);
  1365. return 0;
  1366. }
  1367. static struct pci_device_id sm501_pci_tbl[] = {
  1368. { 0x126f, 0x0501, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  1369. { 0, },
  1370. };
  1371. MODULE_DEVICE_TABLE(pci, sm501_pci_tbl);
  1372. static struct pci_driver sm501_pci_driver = {
  1373. .name = "sm501",
  1374. .id_table = sm501_pci_tbl,
  1375. .probe = sm501_pci_probe,
  1376. .remove = __devexit_p(sm501_pci_remove),
  1377. };
  1378. MODULE_ALIAS("platform:sm501");
  1379. static struct of_device_id __devinitdata of_sm501_match_tbl[] = {
  1380. { .compatible = "smi,sm501", },
  1381. { /* end */ }
  1382. };
  1383. static struct platform_driver sm501_plat_driver = {
  1384. .driver = {
  1385. .name = "sm501",
  1386. .owner = THIS_MODULE,
  1387. .of_match_table = of_sm501_match_tbl,
  1388. },
  1389. .probe = sm501_plat_probe,
  1390. .remove = sm501_plat_remove,
  1391. .suspend = sm501_plat_suspend,
  1392. .resume = sm501_plat_resume,
  1393. };
  1394. static int __init sm501_base_init(void)
  1395. {
  1396. platform_driver_register(&sm501_plat_driver);
  1397. return pci_register_driver(&sm501_pci_driver);
  1398. }
  1399. static void __exit sm501_base_exit(void)
  1400. {
  1401. platform_driver_unregister(&sm501_plat_driver);
  1402. pci_unregister_driver(&sm501_pci_driver);
  1403. }
  1404. module_init(sm501_base_init);
  1405. module_exit(sm501_base_exit);
  1406. MODULE_DESCRIPTION("SM501 Core Driver");
  1407. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Vincent Sanders");
  1408. MODULE_LICENSE("GPL v2");