sm501.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. static inline void sm501_init_reg(struct sm501_devdata *sm,
  660. unsigned long reg,
  661. struct sm501_reg_init *r)
  662. {
  663. unsigned long tmp;
  664. tmp = readl(sm->regs + reg);
  665. tmp |= r->set;
  666. tmp &= ~r->mask;
  667. writel(tmp, sm->regs + reg);
  668. }
  669. /* sm501_init_regs
  670. *
  671. * Setup core register values
  672. */
  673. static void sm501_init_regs(struct sm501_devdata *sm,
  674. struct sm501_initdata *init)
  675. {
  676. sm501_misc_control(sm->dev,
  677. init->misc_control.set,
  678. init->misc_control.mask);
  679. sm501_init_reg(sm, SM501_MISC_TIMING, &init->misc_timing);
  680. sm501_init_reg(sm, SM501_GPIO31_0_CONTROL, &init->gpio_low);
  681. sm501_init_reg(sm, SM501_GPIO63_32_CONTROL, &init->gpio_high);
  682. if (init->mclk) {
  683. dev_info(sm->dev, "setting MCLK to %ld\n", init->mclk);
  684. sm501_set_clock(sm->dev, SM501_CLOCK_MCLK, init->mclk);
  685. }
  686. if (init->m1xclk) {
  687. dev_info(sm->dev, "setting M1XCLK to %ld\n", init->m1xclk);
  688. sm501_set_clock(sm->dev, SM501_CLOCK_M1XCLK, init->m1xclk);
  689. }
  690. }
  691. static unsigned int sm501_mem_local[] = {
  692. [0] = 4*1024*1024,
  693. [1] = 8*1024*1024,
  694. [2] = 16*1024*1024,
  695. [3] = 32*1024*1024,
  696. [4] = 64*1024*1024,
  697. [5] = 2*1024*1024,
  698. };
  699. /* sm501_init_dev
  700. *
  701. * Common init code for an SM501
  702. */
  703. static int sm501_init_dev(struct sm501_devdata *sm)
  704. {
  705. resource_size_t mem_avail;
  706. unsigned long dramctrl;
  707. int ret;
  708. mutex_init(&sm->clock_lock);
  709. spin_lock_init(&sm->reg_lock);
  710. INIT_LIST_HEAD(&sm->devices);
  711. dramctrl = readl(sm->regs + SM501_DRAM_CONTROL);
  712. mem_avail = sm501_mem_local[(dramctrl >> 13) & 0x7];
  713. dev_info(sm->dev, "SM501 At %p: Version %08x, %ld Mb, IRQ %d\n",
  714. sm->regs, readl(sm->regs + SM501_DEVICEID),
  715. (unsigned long)mem_avail >> 20, sm->irq);
  716. sm501_dump_gate(sm);
  717. ret = device_create_file(sm->dev, &dev_attr_dbg_regs);
  718. if (ret)
  719. dev_err(sm->dev, "failed to create debug regs file\n");
  720. sm501_dump_clk(sm);
  721. /* check to see if we have some device initialisation */
  722. if (sm->platdata) {
  723. struct sm501_platdata *pdata = sm->platdata;
  724. if (pdata->init) {
  725. sm501_init_regs(sm, sm->platdata->init);
  726. if (pdata->init->devices & SM501_USE_USB_HOST)
  727. sm501_register_usbhost(sm, &mem_avail);
  728. }
  729. }
  730. /* always create a framebuffer */
  731. sm501_register_display(sm, &mem_avail);
  732. return 0;
  733. }
  734. static int sm501_plat_probe(struct platform_device *dev)
  735. {
  736. struct sm501_devdata *sm;
  737. int err;
  738. sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
  739. if (sm == NULL) {
  740. dev_err(&dev->dev, "no memory for device data\n");
  741. err = -ENOMEM;
  742. goto err1;
  743. }
  744. sm->dev = &dev->dev;
  745. sm->pdev_id = dev->id;
  746. sm->irq = platform_get_irq(dev, 0);
  747. sm->io_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
  748. sm->mem_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
  749. sm->platdata = dev->dev.platform_data;
  750. if (sm->irq < 0) {
  751. dev_err(&dev->dev, "failed to get irq resource\n");
  752. err = sm->irq;
  753. goto err_res;
  754. }
  755. if (sm->io_res == NULL || sm->mem_res == NULL) {
  756. dev_err(&dev->dev, "failed to get IO resource\n");
  757. err = -ENOENT;
  758. goto err_res;
  759. }
  760. sm->regs_claim = request_mem_region(sm->io_res->start,
  761. 0x100, "sm501");
  762. if (sm->regs_claim == NULL) {
  763. dev_err(&dev->dev, "cannot claim registers\n");
  764. err= -EBUSY;
  765. goto err_res;
  766. }
  767. platform_set_drvdata(dev, sm);
  768. sm->regs = ioremap(sm->io_res->start,
  769. (sm->io_res->end - sm->io_res->start) - 1);
  770. if (sm->regs == NULL) {
  771. dev_err(&dev->dev, "cannot remap registers\n");
  772. err = -EIO;
  773. goto err_claim;
  774. }
  775. return sm501_init_dev(sm);
  776. err_claim:
  777. release_resource(sm->regs_claim);
  778. kfree(sm->regs_claim);
  779. err_res:
  780. kfree(sm);
  781. err1:
  782. return err;
  783. }
  784. #ifdef CONFIG_PM
  785. /* power management support */
  786. static int sm501_plat_suspend(struct platform_device *pdev, pm_message_t state)
  787. {
  788. struct sm501_devdata *sm = platform_get_drvdata(pdev);
  789. sm->in_suspend = 1;
  790. sm->pm_misc = readl(sm->regs + SM501_MISC_CONTROL);
  791. sm501_dump_regs(sm);
  792. return 0;
  793. }
  794. static int sm501_plat_resume(struct platform_device *pdev)
  795. {
  796. struct sm501_devdata *sm = platform_get_drvdata(pdev);
  797. sm501_dump_regs(sm);
  798. sm501_dump_gate(sm);
  799. sm501_dump_clk(sm);
  800. /* check to see if we are in the same state as when suspended */
  801. if (readl(sm->regs + SM501_MISC_CONTROL) != sm->pm_misc) {
  802. dev_info(sm->dev, "SM501_MISC_CONTROL changed over sleep\n");
  803. writel(sm->pm_misc, sm->regs + SM501_MISC_CONTROL);
  804. /* our suspend causes the controller state to change,
  805. * either by something attempting setup, power loss,
  806. * or an external reset event on power change */
  807. if (sm->platdata && sm->platdata->init) {
  808. sm501_init_regs(sm, sm->platdata->init);
  809. }
  810. }
  811. /* dump our state from resume */
  812. sm501_dump_regs(sm);
  813. sm501_dump_clk(sm);
  814. sm->in_suspend = 0;
  815. return 0;
  816. }
  817. #else
  818. #define sm501_plat_suspend NULL
  819. #define sm501_plat_resume NULL
  820. #endif
  821. /* Initialisation data for PCI devices */
  822. static struct sm501_initdata sm501_pci_initdata = {
  823. .gpio_high = {
  824. .set = 0x3F000000, /* 24bit panel */
  825. .mask = 0x0,
  826. },
  827. .misc_timing = {
  828. .set = 0x010100, /* SDRAM timing */
  829. .mask = 0x1F1F00,
  830. },
  831. .misc_control = {
  832. .set = SM501_MISC_PNL_24BIT,
  833. .mask = 0,
  834. },
  835. .devices = SM501_USE_ALL,
  836. .mclk = 100 * MHZ,
  837. .m1xclk = 160 * MHZ,
  838. };
  839. static struct sm501_platdata_fbsub sm501_pdata_fbsub = {
  840. .flags = (SM501FB_FLAG_USE_INIT_MODE |
  841. SM501FB_FLAG_USE_HWCURSOR |
  842. SM501FB_FLAG_USE_HWACCEL |
  843. SM501FB_FLAG_DISABLE_AT_EXIT),
  844. };
  845. static struct sm501_platdata_fb sm501_fb_pdata = {
  846. .fb_route = SM501_FB_OWN,
  847. .fb_crt = &sm501_pdata_fbsub,
  848. .fb_pnl = &sm501_pdata_fbsub,
  849. };
  850. static struct sm501_platdata sm501_pci_platdata = {
  851. .init = &sm501_pci_initdata,
  852. .fb = &sm501_fb_pdata,
  853. };
  854. static int sm501_pci_probe(struct pci_dev *dev,
  855. const struct pci_device_id *id)
  856. {
  857. struct sm501_devdata *sm;
  858. int err;
  859. sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
  860. if (sm == NULL) {
  861. dev_err(&dev->dev, "no memory for device data\n");
  862. err = -ENOMEM;
  863. goto err1;
  864. }
  865. /* set a default set of platform data */
  866. dev->dev.platform_data = sm->platdata = &sm501_pci_platdata;
  867. /* set a hopefully unique id for our child platform devices */
  868. sm->pdev_id = 32 + dev->devfn;
  869. pci_set_drvdata(dev, sm);
  870. err = pci_enable_device(dev);
  871. if (err) {
  872. dev_err(&dev->dev, "cannot enable device\n");
  873. goto err2;
  874. }
  875. sm->dev = &dev->dev;
  876. sm->irq = dev->irq;
  877. #ifdef __BIG_ENDIAN
  878. /* if the system is big-endian, we most probably have a
  879. * translation in the IO layer making the PCI bus little endian
  880. * so make the framebuffer swapped pixels */
  881. sm501_fb_pdata.flags |= SM501_FBPD_SWAP_FB_ENDIAN;
  882. #endif
  883. /* check our resources */
  884. if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM)) {
  885. dev_err(&dev->dev, "region #0 is not memory?\n");
  886. err = -EINVAL;
  887. goto err3;
  888. }
  889. if (!(pci_resource_flags(dev, 1) & IORESOURCE_MEM)) {
  890. dev_err(&dev->dev, "region #1 is not memory?\n");
  891. err = -EINVAL;
  892. goto err3;
  893. }
  894. /* make our resources ready for sharing */
  895. sm->io_res = &dev->resource[1];
  896. sm->mem_res = &dev->resource[0];
  897. sm->regs_claim = request_mem_region(sm->io_res->start,
  898. 0x100, "sm501");
  899. if (sm->regs_claim == NULL) {
  900. dev_err(&dev->dev, "cannot claim registers\n");
  901. err= -EBUSY;
  902. goto err3;
  903. }
  904. sm->regs = ioremap(pci_resource_start(dev, 1),
  905. pci_resource_len(dev, 1));
  906. if (sm->regs == NULL) {
  907. dev_err(&dev->dev, "cannot remap registers\n");
  908. err = -EIO;
  909. goto err4;
  910. }
  911. sm501_init_dev(sm);
  912. return 0;
  913. err4:
  914. release_resource(sm->regs_claim);
  915. kfree(sm->regs_claim);
  916. err3:
  917. pci_disable_device(dev);
  918. err2:
  919. pci_set_drvdata(dev, NULL);
  920. kfree(sm);
  921. err1:
  922. return err;
  923. }
  924. static void sm501_remove_sub(struct sm501_devdata *sm,
  925. struct sm501_device *smdev)
  926. {
  927. list_del(&smdev->list);
  928. platform_device_unregister(&smdev->pdev);
  929. }
  930. static void sm501_dev_remove(struct sm501_devdata *sm)
  931. {
  932. struct sm501_device *smdev, *tmp;
  933. list_for_each_entry_safe(smdev, tmp, &sm->devices, list)
  934. sm501_remove_sub(sm, smdev);
  935. device_remove_file(sm->dev, &dev_attr_dbg_regs);
  936. }
  937. static void sm501_pci_remove(struct pci_dev *dev)
  938. {
  939. struct sm501_devdata *sm = pci_get_drvdata(dev);
  940. sm501_dev_remove(sm);
  941. iounmap(sm->regs);
  942. release_resource(sm->regs_claim);
  943. kfree(sm->regs_claim);
  944. pci_set_drvdata(dev, NULL);
  945. pci_disable_device(dev);
  946. }
  947. static int sm501_plat_remove(struct platform_device *dev)
  948. {
  949. struct sm501_devdata *sm = platform_get_drvdata(dev);
  950. sm501_dev_remove(sm);
  951. iounmap(sm->regs);
  952. release_resource(sm->regs_claim);
  953. kfree(sm->regs_claim);
  954. return 0;
  955. }
  956. static struct pci_device_id sm501_pci_tbl[] = {
  957. { 0x126f, 0x0501, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  958. { 0, },
  959. };
  960. MODULE_DEVICE_TABLE(pci, sm501_pci_tbl);
  961. static struct pci_driver sm501_pci_drv = {
  962. .name = "sm501",
  963. .id_table = sm501_pci_tbl,
  964. .probe = sm501_pci_probe,
  965. .remove = sm501_pci_remove,
  966. };
  967. static struct platform_driver sm501_plat_drv = {
  968. .driver = {
  969. .name = "sm501",
  970. .owner = THIS_MODULE,
  971. },
  972. .probe = sm501_plat_probe,
  973. .remove = sm501_plat_remove,
  974. .suspend = sm501_plat_suspend,
  975. .resume = sm501_plat_resume,
  976. };
  977. static int __init sm501_base_init(void)
  978. {
  979. platform_driver_register(&sm501_plat_drv);
  980. return pci_register_driver(&sm501_pci_drv);
  981. }
  982. static void __exit sm501_base_exit(void)
  983. {
  984. platform_driver_unregister(&sm501_plat_drv);
  985. pci_unregister_driver(&sm501_pci_drv);
  986. }
  987. module_init(sm501_base_init);
  988. module_exit(sm501_base_exit);
  989. MODULE_DESCRIPTION("SM501 Core Driver");
  990. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Vincent Sanders");
  991. MODULE_LICENSE("GPL v2");