sm501.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  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/sm501.h>
  22. #include <linux/sm501-regs.h>
  23. #include <asm/io.h>
  24. struct sm501_device {
  25. struct list_head list;
  26. struct platform_device pdev;
  27. };
  28. struct sm501_devdata {
  29. spinlock_t reg_lock;
  30. struct mutex clock_lock;
  31. struct list_head devices;
  32. struct device *dev;
  33. struct resource *io_res;
  34. struct resource *mem_res;
  35. struct resource *regs_claim;
  36. struct sm501_platdata *platdata;
  37. unsigned int in_suspend;
  38. unsigned long pm_misc;
  39. int unit_power[20];
  40. unsigned int pdev_id;
  41. unsigned int irq;
  42. void __iomem *regs;
  43. };
  44. #define MHZ (1000 * 1000)
  45. #ifdef DEBUG
  46. static const unsigned int misc_div[] = {
  47. [0] = 1,
  48. [1] = 2,
  49. [2] = 4,
  50. [3] = 8,
  51. [4] = 16,
  52. [5] = 32,
  53. [6] = 64,
  54. [7] = 128,
  55. [8] = 3,
  56. [9] = 6,
  57. [10] = 12,
  58. [11] = 24,
  59. [12] = 48,
  60. [13] = 96,
  61. [14] = 192,
  62. [15] = 384,
  63. };
  64. static const unsigned int px_div[] = {
  65. [0] = 1,
  66. [1] = 2,
  67. [2] = 4,
  68. [3] = 8,
  69. [4] = 16,
  70. [5] = 32,
  71. [6] = 64,
  72. [7] = 128,
  73. [8] = 3,
  74. [9] = 6,
  75. [10] = 12,
  76. [11] = 24,
  77. [12] = 48,
  78. [13] = 96,
  79. [14] = 192,
  80. [15] = 384,
  81. [16] = 5,
  82. [17] = 10,
  83. [18] = 20,
  84. [19] = 40,
  85. [20] = 80,
  86. [21] = 160,
  87. [22] = 320,
  88. [23] = 604,
  89. };
  90. static unsigned long decode_div(unsigned long pll2, unsigned long val,
  91. unsigned int lshft, unsigned int selbit,
  92. unsigned long mask, const unsigned int *dtab)
  93. {
  94. if (val & selbit)
  95. pll2 = 288 * MHZ;
  96. return pll2 / dtab[(val >> lshft) & mask];
  97. }
  98. #define fmt_freq(x) ((x) / MHZ), ((x) % MHZ), (x)
  99. /* sm501_dump_clk
  100. *
  101. * Print out the current clock configuration for the device
  102. */
  103. static void sm501_dump_clk(struct sm501_devdata *sm)
  104. {
  105. unsigned long misct = readl(sm->regs + SM501_MISC_TIMING);
  106. unsigned long pm0 = readl(sm->regs + SM501_POWER_MODE_0_CLOCK);
  107. unsigned long pm1 = readl(sm->regs + SM501_POWER_MODE_1_CLOCK);
  108. unsigned long pmc = readl(sm->regs + SM501_POWER_MODE_CONTROL);
  109. unsigned long sdclk0, sdclk1;
  110. unsigned long pll2 = 0;
  111. switch (misct & 0x30) {
  112. case 0x00:
  113. pll2 = 336 * MHZ;
  114. break;
  115. case 0x10:
  116. pll2 = 288 * MHZ;
  117. break;
  118. case 0x20:
  119. pll2 = 240 * MHZ;
  120. break;
  121. case 0x30:
  122. pll2 = 192 * MHZ;
  123. break;
  124. }
  125. sdclk0 = (misct & (1<<12)) ? pll2 : 288 * MHZ;
  126. sdclk0 /= misc_div[((misct >> 8) & 0xf)];
  127. sdclk1 = (misct & (1<<20)) ? pll2 : 288 * MHZ;
  128. sdclk1 /= misc_div[((misct >> 16) & 0xf)];
  129. dev_dbg(sm->dev, "MISCT=%08lx, PM0=%08lx, PM1=%08lx\n",
  130. misct, pm0, pm1);
  131. dev_dbg(sm->dev, "PLL2 = %ld.%ld MHz (%ld), SDCLK0=%08lx, SDCLK1=%08lx\n",
  132. fmt_freq(pll2), sdclk0, sdclk1);
  133. dev_dbg(sm->dev, "SDRAM: PM0=%ld, PM1=%ld\n", sdclk0, sdclk1);
  134. dev_dbg(sm->dev, "PM0[%c]: "
  135. "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
  136. x "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
  137. (pmc & 3 ) == 0 ? '*' : '-',
  138. fmt_freq(decode_div(pll2, pm0, 24, 1<<29, 31, px_div)),
  139. fmt_freq(decode_div(pll2, pm0, 16, 1<<20, 15, misc_div)),
  140. fmt_freq(decode_div(pll2, pm0, 8, 1<<12, 15, misc_div)),
  141. fmt_freq(decode_div(pll2, pm0, 0, 1<<4, 15, misc_div)));
  142. dev_dbg(sm->dev, "PM1[%c]: "
  143. "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
  144. "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
  145. (pmc & 3 ) == 1 ? '*' : '-',
  146. fmt_freq(decode_div(pll2, pm1, 24, 1<<29, 31, px_div)),
  147. fmt_freq(decode_div(pll2, pm1, 16, 1<<20, 15, misc_div)),
  148. fmt_freq(decode_div(pll2, pm1, 8, 1<<12, 15, misc_div)),
  149. fmt_freq(decode_div(pll2, pm1, 0, 1<<4, 15, misc_div)));
  150. }
  151. static void sm501_dump_regs(struct sm501_devdata *sm)
  152. {
  153. void __iomem *regs = sm->regs;
  154. dev_info(sm->dev, "System Control %08x\n",
  155. readl(regs + SM501_SYSTEM_CONTROL));
  156. dev_info(sm->dev, "Misc Control %08x\n",
  157. readl(regs + SM501_MISC_CONTROL));
  158. dev_info(sm->dev, "GPIO Control Low %08x\n",
  159. readl(regs + SM501_GPIO31_0_CONTROL));
  160. dev_info(sm->dev, "GPIO Control Hi %08x\n",
  161. readl(regs + SM501_GPIO63_32_CONTROL));
  162. dev_info(sm->dev, "DRAM Control %08x\n",
  163. readl(regs + SM501_DRAM_CONTROL));
  164. dev_info(sm->dev, "Arbitration Ctrl %08x\n",
  165. readl(regs + SM501_ARBTRTN_CONTROL));
  166. dev_info(sm->dev, "Misc Timing %08x\n",
  167. readl(regs + SM501_MISC_TIMING));
  168. }
  169. static void sm501_dump_gate(struct sm501_devdata *sm)
  170. {
  171. dev_info(sm->dev, "CurrentGate %08x\n",
  172. readl(sm->regs + SM501_CURRENT_GATE));
  173. dev_info(sm->dev, "CurrentClock %08x\n",
  174. readl(sm->regs + SM501_CURRENT_CLOCK));
  175. dev_info(sm->dev, "PowerModeControl %08x\n",
  176. readl(sm->regs + SM501_POWER_MODE_CONTROL));
  177. }
  178. #else
  179. static inline void sm501_dump_gate(struct sm501_devdata *sm) { }
  180. static inline void sm501_dump_regs(struct sm501_devdata *sm) { }
  181. static inline void sm501_dump_clk(struct sm501_devdata *sm) { }
  182. #endif
  183. /* sm501_sync_regs
  184. *
  185. * ensure the
  186. */
  187. static void sm501_sync_regs(struct sm501_devdata *sm)
  188. {
  189. readl(sm->regs);
  190. }
  191. static inline void sm501_mdelay(struct sm501_devdata *sm, unsigned int delay)
  192. {
  193. /* during suspend/resume, we are currently not allowed to sleep,
  194. * so change to using mdelay() instead of msleep() if we
  195. * are in one of these paths */
  196. if (sm->in_suspend)
  197. mdelay(delay);
  198. else
  199. msleep(delay);
  200. }
  201. /* sm501_misc_control
  202. *
  203. * alters the miscellaneous control parameters
  204. */
  205. int sm501_misc_control(struct device *dev,
  206. unsigned long set, unsigned long clear)
  207. {
  208. struct sm501_devdata *sm = dev_get_drvdata(dev);
  209. unsigned long misc;
  210. unsigned long save;
  211. unsigned long to;
  212. spin_lock_irqsave(&sm->reg_lock, save);
  213. misc = readl(sm->regs + SM501_MISC_CONTROL);
  214. to = (misc & ~clear) | set;
  215. if (to != misc) {
  216. writel(to, sm->regs + SM501_MISC_CONTROL);
  217. sm501_sync_regs(sm);
  218. dev_dbg(sm->dev, "MISC_CONTROL %08lx\n", misc);
  219. }
  220. spin_unlock_irqrestore(&sm->reg_lock, save);
  221. return to;
  222. }
  223. EXPORT_SYMBOL_GPL(sm501_misc_control);
  224. /* sm501_modify_reg
  225. *
  226. * Modify a register in the SM501 which may be shared with other
  227. * drivers.
  228. */
  229. unsigned long sm501_modify_reg(struct device *dev,
  230. unsigned long reg,
  231. unsigned long set,
  232. unsigned long clear)
  233. {
  234. struct sm501_devdata *sm = dev_get_drvdata(dev);
  235. unsigned long data;
  236. unsigned long save;
  237. spin_lock_irqsave(&sm->reg_lock, save);
  238. data = readl(sm->regs + reg);
  239. data |= set;
  240. data &= ~clear;
  241. writel(data, sm->regs + reg);
  242. sm501_sync_regs(sm);
  243. spin_unlock_irqrestore(&sm->reg_lock, save);
  244. return data;
  245. }
  246. EXPORT_SYMBOL_GPL(sm501_modify_reg);
  247. unsigned long sm501_gpio_get(struct device *dev,
  248. unsigned long gpio)
  249. {
  250. struct sm501_devdata *sm = dev_get_drvdata(dev);
  251. unsigned long result;
  252. unsigned long reg;
  253. reg = (gpio > 32) ? SM501_GPIO_DATA_HIGH : SM501_GPIO_DATA_LOW;
  254. result = readl(sm->regs + reg);
  255. result >>= (gpio & 31);
  256. return result & 1UL;
  257. }
  258. EXPORT_SYMBOL_GPL(sm501_gpio_get);
  259. void sm501_gpio_set(struct device *dev,
  260. unsigned long gpio,
  261. unsigned int to,
  262. unsigned int dir)
  263. {
  264. struct sm501_devdata *sm = dev_get_drvdata(dev);
  265. unsigned long bit = 1 << (gpio & 31);
  266. unsigned long base;
  267. unsigned long save;
  268. unsigned long val;
  269. base = (gpio > 32) ? SM501_GPIO_DATA_HIGH : SM501_GPIO_DATA_LOW;
  270. base += SM501_GPIO;
  271. spin_lock_irqsave(&sm->reg_lock, save);
  272. val = readl(sm->regs + base) & ~bit;
  273. if (to)
  274. val |= bit;
  275. writel(val, sm->regs + base);
  276. val = readl(sm->regs + SM501_GPIO_DDR_LOW) & ~bit;
  277. if (dir)
  278. val |= bit;
  279. writel(val, sm->regs + SM501_GPIO_DDR_LOW);
  280. sm501_sync_regs(sm);
  281. spin_unlock_irqrestore(&sm->reg_lock, save);
  282. }
  283. EXPORT_SYMBOL_GPL(sm501_gpio_set);
  284. /* sm501_unit_power
  285. *
  286. * alters the power active gate to set specific units on or off
  287. */
  288. int sm501_unit_power(struct device *dev, unsigned int unit, unsigned int to)
  289. {
  290. struct sm501_devdata *sm = dev_get_drvdata(dev);
  291. unsigned long mode;
  292. unsigned long gate;
  293. unsigned long clock;
  294. mutex_lock(&sm->clock_lock);
  295. mode = readl(sm->regs + SM501_POWER_MODE_CONTROL);
  296. gate = readl(sm->regs + SM501_CURRENT_GATE);
  297. clock = readl(sm->regs + SM501_CURRENT_CLOCK);
  298. mode &= 3; /* get current power mode */
  299. if (unit >= ARRAY_SIZE(sm->unit_power)) {
  300. dev_err(dev, "%s: bad unit %d\n", __FUNCTION__, unit);
  301. goto already;
  302. }
  303. dev_dbg(sm->dev, "%s: unit %d, cur %d, to %d\n", __FUNCTION__, unit,
  304. sm->unit_power[unit], to);
  305. if (to == 0 && sm->unit_power[unit] == 0) {
  306. dev_err(sm->dev, "unit %d is already shutdown\n", unit);
  307. goto already;
  308. }
  309. sm->unit_power[unit] += to ? 1 : -1;
  310. to = sm->unit_power[unit] ? 1 : 0;
  311. if (to) {
  312. if (gate & (1 << unit))
  313. goto already;
  314. gate |= (1 << unit);
  315. } else {
  316. if (!(gate & (1 << unit)))
  317. goto already;
  318. gate &= ~(1 << unit);
  319. }
  320. switch (mode) {
  321. case 1:
  322. writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
  323. writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
  324. mode = 0;
  325. break;
  326. case 2:
  327. case 0:
  328. writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
  329. writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
  330. mode = 1;
  331. break;
  332. default:
  333. return -1;
  334. }
  335. writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
  336. sm501_sync_regs(sm);
  337. dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
  338. gate, clock, mode);
  339. sm501_mdelay(sm, 16);
  340. already:
  341. mutex_unlock(&sm->clock_lock);
  342. return gate;
  343. }
  344. EXPORT_SYMBOL_GPL(sm501_unit_power);
  345. /* Perform a rounded division. */
  346. static long sm501fb_round_div(long num, long denom)
  347. {
  348. /* n / d + 1 / 2 = (2n + d) / 2d */
  349. return (2 * num + denom) / (2 * denom);
  350. }
  351. /* clock value structure. */
  352. struct sm501_clock {
  353. unsigned long mclk;
  354. int divider;
  355. int shift;
  356. };
  357. /* sm501_select_clock
  358. *
  359. * selects nearest discrete clock frequency the SM501 can achive
  360. * the maximum divisor is 3 or 5
  361. */
  362. static unsigned long sm501_select_clock(unsigned long freq,
  363. struct sm501_clock *clock,
  364. int max_div)
  365. {
  366. unsigned long mclk;
  367. int divider;
  368. int shift;
  369. long diff;
  370. long best_diff = 999999999;
  371. /* Try 288MHz and 336MHz clocks. */
  372. for (mclk = 288000000; mclk <= 336000000; mclk += 48000000) {
  373. /* try dividers 1 and 3 for CRT and for panel,
  374. try divider 5 for panel only.*/
  375. for (divider = 1; divider <= max_div; divider += 2) {
  376. /* try all 8 shift values.*/
  377. for (shift = 0; shift < 8; shift++) {
  378. /* Calculate difference to requested clock */
  379. diff = sm501fb_round_div(mclk, divider << shift) - freq;
  380. if (diff < 0)
  381. diff = -diff;
  382. /* If it is less than the current, use it */
  383. if (diff < best_diff) {
  384. best_diff = diff;
  385. clock->mclk = mclk;
  386. clock->divider = divider;
  387. clock->shift = shift;
  388. }
  389. }
  390. }
  391. }
  392. /* Return best clock. */
  393. return clock->mclk / (clock->divider << clock->shift);
  394. }
  395. /* sm501_set_clock
  396. *
  397. * set one of the four clock sources to the closest available frequency to
  398. * the one specified
  399. */
  400. unsigned long sm501_set_clock(struct device *dev,
  401. int clksrc,
  402. unsigned long req_freq)
  403. {
  404. struct sm501_devdata *sm = dev_get_drvdata(dev);
  405. unsigned long mode = readl(sm->regs + SM501_POWER_MODE_CONTROL);
  406. unsigned long gate = readl(sm->regs + SM501_CURRENT_GATE);
  407. unsigned long clock = readl(sm->regs + SM501_CURRENT_CLOCK);
  408. unsigned char reg;
  409. unsigned long sm501_freq; /* the actual frequency acheived */
  410. struct sm501_clock to;
  411. /* find achivable discrete frequency and setup register value
  412. * accordingly, V2XCLK, MCLK and M1XCLK are the same P2XCLK
  413. * has an extra bit for the divider */
  414. switch (clksrc) {
  415. case SM501_CLOCK_P2XCLK:
  416. /* This clock is divided in half so to achive the
  417. * requested frequency the value must be multiplied by
  418. * 2. This clock also has an additional pre divisor */
  419. sm501_freq = (sm501_select_clock(2 * req_freq, &to, 5) / 2);
  420. reg=to.shift & 0x07;/* bottom 3 bits are shift */
  421. if (to.divider == 3)
  422. reg |= 0x08; /* /3 divider required */
  423. else if (to.divider == 5)
  424. reg |= 0x10; /* /5 divider required */
  425. if (to.mclk != 288000000)
  426. reg |= 0x20; /* which mclk pll is source */
  427. break;
  428. case SM501_CLOCK_V2XCLK:
  429. /* This clock is divided in half so to achive the
  430. * requested frequency the value must be multiplied by 2. */
  431. sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
  432. reg=to.shift & 0x07; /* bottom 3 bits are shift */
  433. if (to.divider == 3)
  434. reg |= 0x08; /* /3 divider required */
  435. if (to.mclk != 288000000)
  436. reg |= 0x10; /* which mclk pll is source */
  437. break;
  438. case SM501_CLOCK_MCLK:
  439. case SM501_CLOCK_M1XCLK:
  440. /* These clocks are the same and not further divided */
  441. sm501_freq = sm501_select_clock( req_freq, &to, 3);
  442. reg=to.shift & 0x07; /* bottom 3 bits are shift */
  443. if (to.divider == 3)
  444. reg |= 0x08; /* /3 divider required */
  445. if (to.mclk != 288000000)
  446. reg |= 0x10; /* which mclk pll is source */
  447. break;
  448. default:
  449. return 0; /* this is bad */
  450. }
  451. mutex_lock(&sm->clock_lock);
  452. mode = readl(sm->regs + SM501_POWER_MODE_CONTROL);
  453. gate = readl(sm->regs + SM501_CURRENT_GATE);
  454. clock = readl(sm->regs + SM501_CURRENT_CLOCK);
  455. clock = clock & ~(0xFF << clksrc);
  456. clock |= reg<<clksrc;
  457. mode &= 3; /* find current mode */
  458. switch (mode) {
  459. case 1:
  460. writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
  461. writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
  462. mode = 0;
  463. break;
  464. case 2:
  465. case 0:
  466. writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
  467. writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
  468. mode = 1;
  469. break;
  470. default:
  471. mutex_unlock(&sm->clock_lock);
  472. return -1;
  473. }
  474. writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
  475. sm501_sync_regs(sm);
  476. dev_info(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
  477. gate, clock, mode);
  478. sm501_mdelay(sm, 16);
  479. mutex_unlock(&sm->clock_lock);
  480. sm501_dump_clk(sm);
  481. return sm501_freq;
  482. }
  483. EXPORT_SYMBOL_GPL(sm501_set_clock);
  484. /* sm501_find_clock
  485. *
  486. * finds the closest available frequency for a given clock
  487. */
  488. unsigned long sm501_find_clock(int clksrc,
  489. unsigned long req_freq)
  490. {
  491. unsigned long sm501_freq; /* the frequency achiveable by the 501 */
  492. struct sm501_clock to;
  493. switch (clksrc) {
  494. case SM501_CLOCK_P2XCLK:
  495. sm501_freq = (sm501_select_clock(2 * req_freq, &to, 5) / 2);
  496. break;
  497. case SM501_CLOCK_V2XCLK:
  498. sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
  499. break;
  500. case SM501_CLOCK_MCLK:
  501. case SM501_CLOCK_M1XCLK:
  502. sm501_freq = sm501_select_clock(req_freq, &to, 3);
  503. break;
  504. default:
  505. sm501_freq = 0; /* error */
  506. }
  507. return sm501_freq;
  508. }
  509. EXPORT_SYMBOL_GPL(sm501_find_clock);
  510. static struct sm501_device *to_sm_device(struct platform_device *pdev)
  511. {
  512. return container_of(pdev, struct sm501_device, pdev);
  513. }
  514. /* sm501_device_release
  515. *
  516. * A release function for the platform devices we create to allow us to
  517. * free any items we allocated
  518. */
  519. static void sm501_device_release(struct device *dev)
  520. {
  521. kfree(to_sm_device(to_platform_device(dev)));
  522. }
  523. /* sm501_create_subdev
  524. *
  525. * Create a skeleton platform device with resources for passing to a
  526. * sub-driver
  527. */
  528. static struct platform_device *
  529. sm501_create_subdev(struct sm501_devdata *sm,
  530. char *name, unsigned int res_count)
  531. {
  532. struct sm501_device *smdev;
  533. smdev = kzalloc(sizeof(struct sm501_device) +
  534. sizeof(struct resource) * res_count, GFP_KERNEL);
  535. if (!smdev)
  536. return NULL;
  537. smdev->pdev.dev.release = sm501_device_release;
  538. smdev->pdev.name = name;
  539. smdev->pdev.id = sm->pdev_id;
  540. smdev->pdev.resource = (struct resource *)(smdev+1);
  541. smdev->pdev.num_resources = res_count;
  542. smdev->pdev.dev.parent = sm->dev;
  543. return &smdev->pdev;
  544. }
  545. /* sm501_register_device
  546. *
  547. * Register a platform device created with sm501_create_subdev()
  548. */
  549. static int sm501_register_device(struct sm501_devdata *sm,
  550. struct platform_device *pdev)
  551. {
  552. struct sm501_device *smdev = to_sm_device(pdev);
  553. int ptr;
  554. int ret;
  555. for (ptr = 0; ptr < pdev->num_resources; ptr++) {
  556. printk("%s[%d] flags %08lx: %08llx..%08llx\n",
  557. pdev->name, ptr,
  558. pdev->resource[ptr].flags,
  559. (unsigned long long)pdev->resource[ptr].start,
  560. (unsigned long long)pdev->resource[ptr].end);
  561. }
  562. ret = platform_device_register(pdev);
  563. if (ret >= 0) {
  564. dev_dbg(sm->dev, "registered %s\n", pdev->name);
  565. list_add_tail(&smdev->list, &sm->devices);
  566. } else
  567. dev_err(sm->dev, "error registering %s (%d)\n",
  568. pdev->name, ret);
  569. return ret;
  570. }
  571. /* sm501_create_subio
  572. *
  573. * Fill in an IO resource for a sub device
  574. */
  575. static void sm501_create_subio(struct sm501_devdata *sm,
  576. struct resource *res,
  577. resource_size_t offs,
  578. resource_size_t size)
  579. {
  580. res->flags = IORESOURCE_MEM;
  581. res->parent = sm->io_res;
  582. res->start = sm->io_res->start + offs;
  583. res->end = res->start + size - 1;
  584. }
  585. /* sm501_create_mem
  586. *
  587. * Fill in an MEM resource for a sub device
  588. */
  589. static void sm501_create_mem(struct sm501_devdata *sm,
  590. struct resource *res,
  591. resource_size_t *offs,
  592. resource_size_t size)
  593. {
  594. *offs -= size; /* adjust memory size */
  595. res->flags = IORESOURCE_MEM;
  596. res->parent = sm->mem_res;
  597. res->start = sm->mem_res->start + *offs;
  598. res->end = res->start + size - 1;
  599. }
  600. /* sm501_create_irq
  601. *
  602. * Fill in an IRQ resource for a sub device
  603. */
  604. static void sm501_create_irq(struct sm501_devdata *sm,
  605. struct resource *res)
  606. {
  607. res->flags = IORESOURCE_IRQ;
  608. res->parent = NULL;
  609. res->start = res->end = sm->irq;
  610. }
  611. static int sm501_register_usbhost(struct sm501_devdata *sm,
  612. resource_size_t *mem_avail)
  613. {
  614. struct platform_device *pdev;
  615. pdev = sm501_create_subdev(sm, "sm501-usb", 3);
  616. if (!pdev)
  617. return -ENOMEM;
  618. sm501_create_subio(sm, &pdev->resource[0], 0x40000, 0x20000);
  619. sm501_create_mem(sm, &pdev->resource[1], mem_avail, 256*1024);
  620. sm501_create_irq(sm, &pdev->resource[2]);
  621. return sm501_register_device(sm, pdev);
  622. }
  623. static int sm501_register_display(struct sm501_devdata *sm,
  624. resource_size_t *mem_avail)
  625. {
  626. struct platform_device *pdev;
  627. pdev = sm501_create_subdev(sm, "sm501-fb", 4);
  628. if (!pdev)
  629. return -ENOMEM;
  630. sm501_create_subio(sm, &pdev->resource[0], 0x80000, 0x10000);
  631. sm501_create_subio(sm, &pdev->resource[1], 0x100000, 0x50000);
  632. sm501_create_mem(sm, &pdev->resource[2], mem_avail, *mem_avail);
  633. sm501_create_irq(sm, &pdev->resource[3]);
  634. return sm501_register_device(sm, pdev);
  635. }
  636. /* sm501_dbg_regs
  637. *
  638. * Debug attribute to attach to parent device to show core registers
  639. */
  640. static ssize_t sm501_dbg_regs(struct device *dev,
  641. struct device_attribute *attr, char *buff)
  642. {
  643. struct sm501_devdata *sm = dev_get_drvdata(dev) ;
  644. unsigned int reg;
  645. char *ptr = buff;
  646. int ret;
  647. for (reg = 0x00; reg < 0x70; reg += 4) {
  648. ret = sprintf(ptr, "%08x = %08x\n",
  649. reg, readl(sm->regs + reg));
  650. ptr += ret;
  651. }
  652. return ptr - buff;
  653. }
  654. static DEVICE_ATTR(dbg_regs, 0666, sm501_dbg_regs, NULL);
  655. /* sm501_init_reg
  656. *
  657. * Helper function for the init code to setup a register
  658. *
  659. * clear the bits which are set in r->mask, and then set
  660. * the bits set in r->set.
  661. */
  662. static inline void sm501_init_reg(struct sm501_devdata *sm,
  663. unsigned long reg,
  664. struct sm501_reg_init *r)
  665. {
  666. unsigned long tmp;
  667. tmp = readl(sm->regs + reg);
  668. tmp &= ~r->mask;
  669. tmp |= r->set;
  670. writel(tmp, sm->regs + reg);
  671. }
  672. /* sm501_init_regs
  673. *
  674. * Setup core register values
  675. */
  676. static void sm501_init_regs(struct sm501_devdata *sm,
  677. struct sm501_initdata *init)
  678. {
  679. sm501_misc_control(sm->dev,
  680. init->misc_control.set,
  681. init->misc_control.mask);
  682. sm501_init_reg(sm, SM501_MISC_TIMING, &init->misc_timing);
  683. sm501_init_reg(sm, SM501_GPIO31_0_CONTROL, &init->gpio_low);
  684. sm501_init_reg(sm, SM501_GPIO63_32_CONTROL, &init->gpio_high);
  685. if (init->m1xclk) {
  686. dev_info(sm->dev, "setting M1XCLK to %ld\n", init->m1xclk);
  687. sm501_set_clock(sm->dev, SM501_CLOCK_M1XCLK, init->m1xclk);
  688. }
  689. if (init->mclk) {
  690. dev_info(sm->dev, "setting MCLK to %ld\n", init->mclk);
  691. sm501_set_clock(sm->dev, SM501_CLOCK_MCLK, init->mclk);
  692. }
  693. }
  694. /* Check the PLL sources for the M1CLK and M1XCLK
  695. *
  696. * If the M1CLK and M1XCLKs are not sourced from the same PLL, then
  697. * there is a risk (see errata AB-5) that the SM501 will cease proper
  698. * function. If this happens, then it is likely the SM501 will
  699. * hang the system.
  700. */
  701. static int sm501_check_clocks(struct sm501_devdata *sm)
  702. {
  703. unsigned long pwrmode = readl(sm->regs + SM501_CURRENT_CLOCK);
  704. unsigned long msrc = (pwrmode & SM501_POWERMODE_M_SRC);
  705. unsigned long m1src = (pwrmode & SM501_POWERMODE_M1_SRC);
  706. return ((msrc == 0 && m1src != 0) || (msrc != 0 && m1src == 0));
  707. }
  708. static unsigned int sm501_mem_local[] = {
  709. [0] = 4*1024*1024,
  710. [1] = 8*1024*1024,
  711. [2] = 16*1024*1024,
  712. [3] = 32*1024*1024,
  713. [4] = 64*1024*1024,
  714. [5] = 2*1024*1024,
  715. };
  716. /* sm501_init_dev
  717. *
  718. * Common init code for an SM501
  719. */
  720. static int sm501_init_dev(struct sm501_devdata *sm)
  721. {
  722. resource_size_t mem_avail;
  723. unsigned long dramctrl;
  724. unsigned long devid;
  725. int ret;
  726. mutex_init(&sm->clock_lock);
  727. spin_lock_init(&sm->reg_lock);
  728. INIT_LIST_HEAD(&sm->devices);
  729. devid = readl(sm->regs + SM501_DEVICEID);
  730. if ((devid & SM501_DEVICEID_IDMASK) != SM501_DEVICEID_SM501) {
  731. dev_err(sm->dev, "incorrect device id %08lx\n", devid);
  732. return -EINVAL;
  733. }
  734. dramctrl = readl(sm->regs + SM501_DRAM_CONTROL);
  735. mem_avail = sm501_mem_local[(dramctrl >> 13) & 0x7];
  736. dev_info(sm->dev, "SM501 At %p: Version %08lx, %ld Mb, IRQ %d\n",
  737. sm->regs, devid, (unsigned long)mem_avail >> 20, sm->irq);
  738. sm501_dump_gate(sm);
  739. ret = device_create_file(sm->dev, &dev_attr_dbg_regs);
  740. if (ret)
  741. dev_err(sm->dev, "failed to create debug regs file\n");
  742. sm501_dump_clk(sm);
  743. /* check to see if we have some device initialisation */
  744. if (sm->platdata) {
  745. struct sm501_platdata *pdata = sm->platdata;
  746. if (pdata->init) {
  747. sm501_init_regs(sm, sm->platdata->init);
  748. if (pdata->init->devices & SM501_USE_USB_HOST)
  749. sm501_register_usbhost(sm, &mem_avail);
  750. }
  751. }
  752. ret = sm501_check_clocks(sm);
  753. if (ret) {
  754. dev_err(sm->dev, "M1X and M clocks sourced from different "
  755. "PLLs\n");
  756. return -EINVAL;
  757. }
  758. /* always create a framebuffer */
  759. sm501_register_display(sm, &mem_avail);
  760. return 0;
  761. }
  762. static int sm501_plat_probe(struct platform_device *dev)
  763. {
  764. struct sm501_devdata *sm;
  765. int err;
  766. sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
  767. if (sm == NULL) {
  768. dev_err(&dev->dev, "no memory for device data\n");
  769. err = -ENOMEM;
  770. goto err1;
  771. }
  772. sm->dev = &dev->dev;
  773. sm->pdev_id = dev->id;
  774. sm->irq = platform_get_irq(dev, 0);
  775. sm->io_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  776. sm->mem_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  777. sm->platdata = dev->dev.platform_data;
  778. if (sm->irq < 0) {
  779. dev_err(&dev->dev, "failed to get irq resource\n");
  780. err = sm->irq;
  781. goto err_res;
  782. }
  783. if (sm->io_res == NULL || sm->mem_res == NULL) {
  784. dev_err(&dev->dev, "failed to get IO resource\n");
  785. err = -ENOENT;
  786. goto err_res;
  787. }
  788. sm->regs_claim = request_mem_region(sm->io_res->start,
  789. 0x100, "sm501");
  790. if (sm->regs_claim == NULL) {
  791. dev_err(&dev->dev, "cannot claim registers\n");
  792. err= -EBUSY;
  793. goto err_res;
  794. }
  795. platform_set_drvdata(dev, sm);
  796. sm->regs = ioremap(sm->io_res->start,
  797. (sm->io_res->end - sm->io_res->start) - 1);
  798. if (sm->regs == NULL) {
  799. dev_err(&dev->dev, "cannot remap registers\n");
  800. err = -EIO;
  801. goto err_claim;
  802. }
  803. return sm501_init_dev(sm);
  804. err_claim:
  805. release_resource(sm->regs_claim);
  806. kfree(sm->regs_claim);
  807. err_res:
  808. kfree(sm);
  809. err1:
  810. return err;
  811. }
  812. #ifdef CONFIG_PM
  813. /* power management support */
  814. static int sm501_plat_suspend(struct platform_device *pdev, pm_message_t state)
  815. {
  816. struct sm501_devdata *sm = platform_get_drvdata(pdev);
  817. sm->in_suspend = 1;
  818. sm->pm_misc = readl(sm->regs + SM501_MISC_CONTROL);
  819. sm501_dump_regs(sm);
  820. return 0;
  821. }
  822. static int sm501_plat_resume(struct platform_device *pdev)
  823. {
  824. struct sm501_devdata *sm = platform_get_drvdata(pdev);
  825. sm501_dump_regs(sm);
  826. sm501_dump_gate(sm);
  827. sm501_dump_clk(sm);
  828. /* check to see if we are in the same state as when suspended */
  829. if (readl(sm->regs + SM501_MISC_CONTROL) != sm->pm_misc) {
  830. dev_info(sm->dev, "SM501_MISC_CONTROL changed over sleep\n");
  831. writel(sm->pm_misc, sm->regs + SM501_MISC_CONTROL);
  832. /* our suspend causes the controller state to change,
  833. * either by something attempting setup, power loss,
  834. * or an external reset event on power change */
  835. if (sm->platdata && sm->platdata->init) {
  836. sm501_init_regs(sm, sm->platdata->init);
  837. }
  838. }
  839. /* dump our state from resume */
  840. sm501_dump_regs(sm);
  841. sm501_dump_clk(sm);
  842. sm->in_suspend = 0;
  843. return 0;
  844. }
  845. #else
  846. #define sm501_plat_suspend NULL
  847. #define sm501_plat_resume NULL
  848. #endif
  849. /* Initialisation data for PCI devices */
  850. static struct sm501_initdata sm501_pci_initdata = {
  851. .gpio_high = {
  852. .set = 0x3F000000, /* 24bit panel */
  853. .mask = 0x0,
  854. },
  855. .misc_timing = {
  856. .set = 0x010100, /* SDRAM timing */
  857. .mask = 0x1F1F00,
  858. },
  859. .misc_control = {
  860. .set = SM501_MISC_PNL_24BIT,
  861. .mask = 0,
  862. },
  863. .devices = SM501_USE_ALL,
  864. /* Errata AB-3 says that 72MHz is the fastest available
  865. * for 33MHZ PCI with proper bus-mastering operation */
  866. .mclk = 72 * MHZ,
  867. .m1xclk = 144 * MHZ,
  868. };
  869. static struct sm501_platdata_fbsub sm501_pdata_fbsub = {
  870. .flags = (SM501FB_FLAG_USE_INIT_MODE |
  871. SM501FB_FLAG_USE_HWCURSOR |
  872. SM501FB_FLAG_USE_HWACCEL |
  873. SM501FB_FLAG_DISABLE_AT_EXIT),
  874. };
  875. static struct sm501_platdata_fb sm501_fb_pdata = {
  876. .fb_route = SM501_FB_OWN,
  877. .fb_crt = &sm501_pdata_fbsub,
  878. .fb_pnl = &sm501_pdata_fbsub,
  879. };
  880. static struct sm501_platdata sm501_pci_platdata = {
  881. .init = &sm501_pci_initdata,
  882. .fb = &sm501_fb_pdata,
  883. };
  884. static int sm501_pci_probe(struct pci_dev *dev,
  885. const struct pci_device_id *id)
  886. {
  887. struct sm501_devdata *sm;
  888. int err;
  889. sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
  890. if (sm == NULL) {
  891. dev_err(&dev->dev, "no memory for device data\n");
  892. err = -ENOMEM;
  893. goto err1;
  894. }
  895. /* set a default set of platform data */
  896. dev->dev.platform_data = sm->platdata = &sm501_pci_platdata;
  897. /* set a hopefully unique id for our child platform devices */
  898. sm->pdev_id = 32 + dev->devfn;
  899. pci_set_drvdata(dev, sm);
  900. err = pci_enable_device(dev);
  901. if (err) {
  902. dev_err(&dev->dev, "cannot enable device\n");
  903. goto err2;
  904. }
  905. sm->dev = &dev->dev;
  906. sm->irq = dev->irq;
  907. #ifdef __BIG_ENDIAN
  908. /* if the system is big-endian, we most probably have a
  909. * translation in the IO layer making the PCI bus little endian
  910. * so make the framebuffer swapped pixels */
  911. sm501_fb_pdata.flags |= SM501_FBPD_SWAP_FB_ENDIAN;
  912. #endif
  913. /* check our resources */
  914. if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM)) {
  915. dev_err(&dev->dev, "region #0 is not memory?\n");
  916. err = -EINVAL;
  917. goto err3;
  918. }
  919. if (!(pci_resource_flags(dev, 1) & IORESOURCE_MEM)) {
  920. dev_err(&dev->dev, "region #1 is not memory?\n");
  921. err = -EINVAL;
  922. goto err3;
  923. }
  924. /* make our resources ready for sharing */
  925. sm->io_res = &dev->resource[1];
  926. sm->mem_res = &dev->resource[0];
  927. sm->regs_claim = request_mem_region(sm->io_res->start,
  928. 0x100, "sm501");
  929. if (sm->regs_claim == NULL) {
  930. dev_err(&dev->dev, "cannot claim registers\n");
  931. err= -EBUSY;
  932. goto err3;
  933. }
  934. sm->regs = ioremap(pci_resource_start(dev, 1),
  935. pci_resource_len(dev, 1));
  936. if (sm->regs == NULL) {
  937. dev_err(&dev->dev, "cannot remap registers\n");
  938. err = -EIO;
  939. goto err4;
  940. }
  941. sm501_init_dev(sm);
  942. return 0;
  943. err4:
  944. release_resource(sm->regs_claim);
  945. kfree(sm->regs_claim);
  946. err3:
  947. pci_disable_device(dev);
  948. err2:
  949. pci_set_drvdata(dev, NULL);
  950. kfree(sm);
  951. err1:
  952. return err;
  953. }
  954. static void sm501_remove_sub(struct sm501_devdata *sm,
  955. struct sm501_device *smdev)
  956. {
  957. list_del(&smdev->list);
  958. platform_device_unregister(&smdev->pdev);
  959. }
  960. static void sm501_dev_remove(struct sm501_devdata *sm)
  961. {
  962. struct sm501_device *smdev, *tmp;
  963. list_for_each_entry_safe(smdev, tmp, &sm->devices, list)
  964. sm501_remove_sub(sm, smdev);
  965. device_remove_file(sm->dev, &dev_attr_dbg_regs);
  966. }
  967. static void sm501_pci_remove(struct pci_dev *dev)
  968. {
  969. struct sm501_devdata *sm = pci_get_drvdata(dev);
  970. sm501_dev_remove(sm);
  971. iounmap(sm->regs);
  972. release_resource(sm->regs_claim);
  973. kfree(sm->regs_claim);
  974. pci_set_drvdata(dev, NULL);
  975. pci_disable_device(dev);
  976. }
  977. static int sm501_plat_remove(struct platform_device *dev)
  978. {
  979. struct sm501_devdata *sm = platform_get_drvdata(dev);
  980. sm501_dev_remove(sm);
  981. iounmap(sm->regs);
  982. release_resource(sm->regs_claim);
  983. kfree(sm->regs_claim);
  984. return 0;
  985. }
  986. static struct pci_device_id sm501_pci_tbl[] = {
  987. { 0x126f, 0x0501, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  988. { 0, },
  989. };
  990. MODULE_DEVICE_TABLE(pci, sm501_pci_tbl);
  991. static struct pci_driver sm501_pci_drv = {
  992. .name = "sm501",
  993. .id_table = sm501_pci_tbl,
  994. .probe = sm501_pci_probe,
  995. .remove = sm501_pci_remove,
  996. };
  997. static struct platform_driver sm501_plat_drv = {
  998. .driver = {
  999. .name = "sm501",
  1000. .owner = THIS_MODULE,
  1001. },
  1002. .probe = sm501_plat_probe,
  1003. .remove = sm501_plat_remove,
  1004. .suspend = sm501_plat_suspend,
  1005. .resume = sm501_plat_resume,
  1006. };
  1007. static int __init sm501_base_init(void)
  1008. {
  1009. platform_driver_register(&sm501_plat_drv);
  1010. return pci_register_driver(&sm501_pci_drv);
  1011. }
  1012. static void __exit sm501_base_exit(void)
  1013. {
  1014. platform_driver_unregister(&sm501_plat_drv);
  1015. pci_unregister_driver(&sm501_pci_drv);
  1016. }
  1017. module_init(sm501_base_init);
  1018. module_exit(sm501_base_exit);
  1019. MODULE_DESCRIPTION("SM501 Core Driver");
  1020. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Vincent Sanders");
  1021. MODULE_LICENSE("GPL v2");