sm501.c 40 KB

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