soc_common.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*======================================================================
  2. Common support code for the PCMCIA control functionality of
  3. integrated SOCs like the SA-11x0 and PXA2xx microprocessors.
  4. The contents of this file are subject to the Mozilla Public
  5. License Version 1.1 (the "License"); you may not use this file
  6. except in compliance with the License. You may obtain a copy of
  7. the License at http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS
  9. IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10. implied. See the License for the specific language governing
  11. rights and limitations under the License.
  12. The initial developer of the original code is John G. Dorsey
  13. <john+@cs.cmu.edu>. Portions created by John G. Dorsey are
  14. Copyright (C) 1999 John G. Dorsey. All Rights Reserved.
  15. Alternatively, the contents of this file may be used under the
  16. terms of the GNU Public License version 2 (the "GPL"), in which
  17. case the provisions of the GPL are applicable instead of the
  18. above. If you wish to allow the use of your version of this file
  19. only under the terms of the GPL and not to allow others to use
  20. your version of this file under the MPL, indicate your decision
  21. by deleting the provisions above and replace them with the notice
  22. and other provisions required by the GPL. If you do not delete
  23. the provisions above, a recipient may use your version of this
  24. file under either the MPL or the GPL.
  25. ======================================================================*/
  26. #include <linux/cpufreq.h>
  27. #include <linux/gpio.h>
  28. #include <linux/gpio/consumer.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/io.h>
  32. #include <linux/irq.h>
  33. #include <linux/kernel.h>
  34. #include <linux/mm.h>
  35. #include <linux/module.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/mutex.h>
  38. #include <linux/regulator/consumer.h>
  39. #include <linux/spinlock.h>
  40. #include <linux/timer.h>
  41. #include <mach/hardware.h>
  42. #include "soc_common.h"
  43. static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev);
  44. #ifdef CONFIG_PCMCIA_DEBUG
  45. static int pc_debug;
  46. module_param(pc_debug, int, 0644);
  47. void soc_pcmcia_debug(struct soc_pcmcia_socket *skt, const char *func,
  48. int lvl, const char *fmt, ...)
  49. {
  50. struct va_format vaf;
  51. va_list args;
  52. if (pc_debug > lvl) {
  53. va_start(args, fmt);
  54. vaf.fmt = fmt;
  55. vaf.va = &args;
  56. printk(KERN_DEBUG "skt%u: %s: %pV", skt->nr, func, &vaf);
  57. va_end(args);
  58. }
  59. }
  60. EXPORT_SYMBOL(soc_pcmcia_debug);
  61. #endif
  62. #define to_soc_pcmcia_socket(x) \
  63. container_of(x, struct soc_pcmcia_socket, socket)
  64. int soc_pcmcia_regulator_set(struct soc_pcmcia_socket *skt,
  65. struct soc_pcmcia_regulator *r, int v)
  66. {
  67. bool on;
  68. int ret;
  69. if (!r->reg)
  70. return 0;
  71. on = v != 0;
  72. if (r->on == on)
  73. return 0;
  74. if (on) {
  75. ret = regulator_set_voltage(r->reg, v * 100000, v * 100000);
  76. if (ret) {
  77. int vout = regulator_get_voltage(r->reg) / 100000;
  78. dev_warn(&skt->socket.dev,
  79. "CS requested %s=%u.%uV, applying %u.%uV\n",
  80. r == &skt->vcc ? "Vcc" : "Vpp",
  81. v / 10, v % 10, vout / 10, vout % 10);
  82. }
  83. ret = regulator_enable(r->reg);
  84. } else {
  85. ret = regulator_disable(r->reg);
  86. }
  87. if (ret == 0)
  88. r->on = on;
  89. return ret;
  90. }
  91. EXPORT_SYMBOL_GPL(soc_pcmcia_regulator_set);
  92. static unsigned short
  93. calc_speed(unsigned short *spds, int num, unsigned short dflt)
  94. {
  95. unsigned short speed = 0;
  96. int i;
  97. for (i = 0; i < num; i++)
  98. if (speed < spds[i])
  99. speed = spds[i];
  100. if (speed == 0)
  101. speed = dflt;
  102. return speed;
  103. }
  104. void soc_common_pcmcia_get_timing(struct soc_pcmcia_socket *skt,
  105. struct soc_pcmcia_timing *timing)
  106. {
  107. timing->io =
  108. calc_speed(skt->spd_io, MAX_IO_WIN, SOC_PCMCIA_IO_ACCESS);
  109. timing->mem =
  110. calc_speed(skt->spd_mem, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
  111. timing->attr =
  112. calc_speed(skt->spd_attr, MAX_WIN, SOC_PCMCIA_3V_MEM_ACCESS);
  113. }
  114. EXPORT_SYMBOL(soc_common_pcmcia_get_timing);
  115. static void __soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt,
  116. unsigned int nr)
  117. {
  118. unsigned int i;
  119. for (i = 0; i < nr; i++)
  120. if (skt->stat[i].irq)
  121. free_irq(skt->stat[i].irq, skt);
  122. if (skt->ops->hw_shutdown)
  123. skt->ops->hw_shutdown(skt);
  124. clk_disable_unprepare(skt->clk);
  125. }
  126. static void soc_pcmcia_hw_shutdown(struct soc_pcmcia_socket *skt)
  127. {
  128. __soc_pcmcia_hw_shutdown(skt, ARRAY_SIZE(skt->stat));
  129. }
  130. int soc_pcmcia_request_gpiods(struct soc_pcmcia_socket *skt)
  131. {
  132. struct device *dev = skt->socket.dev.parent;
  133. struct gpio_desc *desc;
  134. int i;
  135. for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
  136. if (!skt->stat[i].name)
  137. continue;
  138. desc = devm_gpiod_get(dev, skt->stat[i].name, GPIOD_IN);
  139. if (IS_ERR(desc)) {
  140. dev_err(dev, "Failed to get GPIO for %s: %ld\n",
  141. skt->stat[i].name, PTR_ERR(desc));
  142. return PTR_ERR(desc);
  143. }
  144. skt->stat[i].desc = desc;
  145. }
  146. return 0;
  147. }
  148. EXPORT_SYMBOL_GPL(soc_pcmcia_request_gpiods);
  149. static int soc_pcmcia_hw_init(struct soc_pcmcia_socket *skt)
  150. {
  151. int ret = 0, i;
  152. ret = clk_prepare_enable(skt->clk);
  153. if (ret)
  154. return ret;
  155. if (skt->ops->hw_init) {
  156. ret = skt->ops->hw_init(skt);
  157. if (ret) {
  158. clk_disable_unprepare(skt->clk);
  159. return ret;
  160. }
  161. }
  162. for (i = 0; i < ARRAY_SIZE(skt->stat); i++) {
  163. if (gpio_is_valid(skt->stat[i].gpio)) {
  164. unsigned long flags = GPIOF_IN;
  165. /* CD is active low by default */
  166. if (i == SOC_STAT_CD)
  167. flags |= GPIOF_ACTIVE_LOW;
  168. ret = devm_gpio_request_one(skt->socket.dev.parent,
  169. skt->stat[i].gpio, flags,
  170. skt->stat[i].name);
  171. if (ret) {
  172. __soc_pcmcia_hw_shutdown(skt, i);
  173. return ret;
  174. }
  175. skt->stat[i].desc = gpio_to_desc(skt->stat[i].gpio);
  176. }
  177. if (i < SOC_STAT_VS1 && skt->stat[i].desc) {
  178. int irq = gpiod_to_irq(skt->stat[i].desc);
  179. if (irq > 0) {
  180. if (i == SOC_STAT_RDY)
  181. skt->socket.pci_irq = irq;
  182. else
  183. skt->stat[i].irq = irq;
  184. }
  185. }
  186. if (skt->stat[i].irq) {
  187. ret = request_irq(skt->stat[i].irq,
  188. soc_common_pcmcia_interrupt,
  189. IRQF_TRIGGER_NONE,
  190. skt->stat[i].name, skt);
  191. if (ret) {
  192. __soc_pcmcia_hw_shutdown(skt, i);
  193. return ret;
  194. }
  195. }
  196. }
  197. return ret;
  198. }
  199. static void soc_pcmcia_hw_enable(struct soc_pcmcia_socket *skt)
  200. {
  201. int i;
  202. for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
  203. if (skt->stat[i].irq) {
  204. irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_RISING);
  205. irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_EDGE_BOTH);
  206. }
  207. }
  208. static void soc_pcmcia_hw_disable(struct soc_pcmcia_socket *skt)
  209. {
  210. int i;
  211. for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
  212. if (skt->stat[i].irq)
  213. irq_set_irq_type(skt->stat[i].irq, IRQ_TYPE_NONE);
  214. }
  215. /*
  216. * The CF 3.0 specification says that cards tie VS1 to ground and leave
  217. * VS2 open. Many implementations do not wire up the VS signals, so we
  218. * provide hard-coded values as per the CF 3.0 spec.
  219. */
  220. void soc_common_cf_socket_state(struct soc_pcmcia_socket *skt,
  221. struct pcmcia_state *state)
  222. {
  223. state->vs_3v = 1;
  224. }
  225. EXPORT_SYMBOL_GPL(soc_common_cf_socket_state);
  226. static unsigned int soc_common_pcmcia_skt_state(struct soc_pcmcia_socket *skt)
  227. {
  228. struct pcmcia_state state;
  229. unsigned int stat;
  230. memset(&state, 0, sizeof(struct pcmcia_state));
  231. /* Make battery voltage state report 'good' */
  232. state.bvd1 = 1;
  233. state.bvd2 = 1;
  234. if (skt->stat[SOC_STAT_CD].desc)
  235. state.detect = !!gpiod_get_value(skt->stat[SOC_STAT_CD].desc);
  236. if (skt->stat[SOC_STAT_RDY].desc)
  237. state.ready = !!gpiod_get_value(skt->stat[SOC_STAT_RDY].desc);
  238. if (skt->stat[SOC_STAT_BVD1].desc)
  239. state.bvd1 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD1].desc);
  240. if (skt->stat[SOC_STAT_BVD2].desc)
  241. state.bvd2 = !!gpiod_get_value(skt->stat[SOC_STAT_BVD2].desc);
  242. if (skt->stat[SOC_STAT_VS1].desc)
  243. state.vs_3v = !!gpiod_get_value(skt->stat[SOC_STAT_VS1].desc);
  244. if (skt->stat[SOC_STAT_VS2].desc)
  245. state.vs_Xv = !!gpiod_get_value(skt->stat[SOC_STAT_VS2].desc);
  246. skt->ops->socket_state(skt, &state);
  247. stat = state.detect ? SS_DETECT : 0;
  248. stat |= state.ready ? SS_READY : 0;
  249. stat |= state.wrprot ? SS_WRPROT : 0;
  250. stat |= state.vs_3v ? SS_3VCARD : 0;
  251. stat |= state.vs_Xv ? SS_XVCARD : 0;
  252. /* The power status of individual sockets is not available
  253. * explicitly from the hardware, so we just remember the state
  254. * and regurgitate it upon request:
  255. */
  256. stat |= skt->cs_state.Vcc ? SS_POWERON : 0;
  257. if (skt->cs_state.flags & SS_IOCARD)
  258. stat |= state.bvd1 ? 0 : SS_STSCHG;
  259. else {
  260. if (state.bvd1 == 0)
  261. stat |= SS_BATDEAD;
  262. else if (state.bvd2 == 0)
  263. stat |= SS_BATWARN;
  264. }
  265. return stat;
  266. }
  267. /*
  268. * soc_common_pcmcia_config_skt
  269. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  270. *
  271. * Convert PCMCIA socket state to our socket configure structure.
  272. */
  273. static int soc_common_pcmcia_config_skt(
  274. struct soc_pcmcia_socket *skt, socket_state_t *state)
  275. {
  276. int ret;
  277. ret = skt->ops->configure_socket(skt, state);
  278. if (ret < 0) {
  279. pr_err("soc_common_pcmcia: unable to configure socket %d\n",
  280. skt->nr);
  281. /* restore the previous state */
  282. WARN_ON(skt->ops->configure_socket(skt, &skt->cs_state));
  283. return ret;
  284. }
  285. if (ret == 0) {
  286. struct gpio_desc *descs[2];
  287. int values[2], n = 0;
  288. if (skt->gpio_reset) {
  289. descs[n] = skt->gpio_reset;
  290. values[n++] = !!(state->flags & SS_RESET);
  291. }
  292. if (skt->gpio_bus_enable) {
  293. descs[n] = skt->gpio_bus_enable;
  294. values[n++] = !!(state->flags & SS_OUTPUT_ENA);
  295. }
  296. if (n)
  297. gpiod_set_array_value_cansleep(n, descs, values);
  298. /*
  299. * This really needs a better solution. The IRQ
  300. * may or may not be claimed by the driver.
  301. */
  302. if (skt->irq_state != 1 && state->io_irq) {
  303. skt->irq_state = 1;
  304. irq_set_irq_type(skt->socket.pci_irq,
  305. IRQ_TYPE_EDGE_FALLING);
  306. } else if (skt->irq_state == 1 && state->io_irq == 0) {
  307. skt->irq_state = 0;
  308. irq_set_irq_type(skt->socket.pci_irq, IRQ_TYPE_NONE);
  309. }
  310. skt->cs_state = *state;
  311. }
  312. return ret;
  313. }
  314. /* soc_common_pcmcia_sock_init()
  315. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  316. *
  317. * (Re-)Initialise the socket, turning on status interrupts
  318. * and PCMCIA bus. This must wait for power to stabilise
  319. * so that the card status signals report correctly.
  320. *
  321. * Returns: 0
  322. */
  323. static int soc_common_pcmcia_sock_init(struct pcmcia_socket *sock)
  324. {
  325. struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
  326. debug(skt, 2, "initializing socket\n");
  327. if (skt->ops->socket_init)
  328. skt->ops->socket_init(skt);
  329. soc_pcmcia_hw_enable(skt);
  330. return 0;
  331. }
  332. /*
  333. * soc_common_pcmcia_suspend()
  334. * ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  335. *
  336. * Remove power on the socket, disable IRQs from the card.
  337. * Turn off status interrupts, and disable the PCMCIA bus.
  338. *
  339. * Returns: 0
  340. */
  341. static int soc_common_pcmcia_suspend(struct pcmcia_socket *sock)
  342. {
  343. struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
  344. debug(skt, 2, "suspending socket\n");
  345. soc_pcmcia_hw_disable(skt);
  346. if (skt->ops->socket_suspend)
  347. skt->ops->socket_suspend(skt);
  348. return 0;
  349. }
  350. static DEFINE_SPINLOCK(status_lock);
  351. static void soc_common_check_status(struct soc_pcmcia_socket *skt)
  352. {
  353. unsigned int events;
  354. debug(skt, 4, "entering PCMCIA monitoring thread\n");
  355. do {
  356. unsigned int status;
  357. unsigned long flags;
  358. status = soc_common_pcmcia_skt_state(skt);
  359. spin_lock_irqsave(&status_lock, flags);
  360. events = (status ^ skt->status) & skt->cs_state.csc_mask;
  361. skt->status = status;
  362. spin_unlock_irqrestore(&status_lock, flags);
  363. debug(skt, 4, "events: %s%s%s%s%s%s\n",
  364. events == 0 ? "<NONE>" : "",
  365. events & SS_DETECT ? "DETECT " : "",
  366. events & SS_READY ? "READY " : "",
  367. events & SS_BATDEAD ? "BATDEAD " : "",
  368. events & SS_BATWARN ? "BATWARN " : "",
  369. events & SS_STSCHG ? "STSCHG " : "");
  370. if (events)
  371. pcmcia_parse_events(&skt->socket, events);
  372. } while (events);
  373. }
  374. /* Let's poll for events in addition to IRQs since IRQ only is unreliable... */
  375. static void soc_common_pcmcia_poll_event(struct timer_list *t)
  376. {
  377. struct soc_pcmcia_socket *skt = from_timer(skt, t, poll_timer);
  378. debug(skt, 4, "polling for events\n");
  379. mod_timer(&skt->poll_timer, jiffies + SOC_PCMCIA_POLL_PERIOD);
  380. soc_common_check_status(skt);
  381. }
  382. /*
  383. * Service routine for socket driver interrupts (requested by the
  384. * low-level PCMCIA init() operation via soc_common_pcmcia_thread()).
  385. * The actual interrupt-servicing work is performed by
  386. * soc_common_pcmcia_thread(), largely because the Card Services event-
  387. * handling code performs scheduling operations which cannot be
  388. * executed from within an interrupt context.
  389. */
  390. static irqreturn_t soc_common_pcmcia_interrupt(int irq, void *dev)
  391. {
  392. struct soc_pcmcia_socket *skt = dev;
  393. debug(skt, 3, "servicing IRQ %d\n", irq);
  394. soc_common_check_status(skt);
  395. return IRQ_HANDLED;
  396. }
  397. /*
  398. * Implements the get_status() operation for the in-kernel PCMCIA
  399. * service (formerly SS_GetStatus in Card Services). Essentially just
  400. * fills in bits in `status' according to internal driver state or
  401. * the value of the voltage detect chipselect register.
  402. *
  403. * As a debugging note, during card startup, the PCMCIA core issues
  404. * three set_socket() commands in a row the first with RESET deasserted,
  405. * the second with RESET asserted, and the last with RESET deasserted
  406. * again. Following the third set_socket(), a get_status() command will
  407. * be issued. The kernel is looking for the SS_READY flag (see
  408. * setup_socket(), reset_socket(), and unreset_socket() in cs.c).
  409. *
  410. * Returns: 0
  411. */
  412. static int
  413. soc_common_pcmcia_get_status(struct pcmcia_socket *sock, unsigned int *status)
  414. {
  415. struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
  416. skt->status = soc_common_pcmcia_skt_state(skt);
  417. *status = skt->status;
  418. return 0;
  419. }
  420. /*
  421. * Implements the set_socket() operation for the in-kernel PCMCIA
  422. * service (formerly SS_SetSocket in Card Services). We more or
  423. * less punt all of this work and let the kernel handle the details
  424. * of power configuration, reset, &c. We also record the value of
  425. * `state' in order to regurgitate it to the PCMCIA core later.
  426. */
  427. static int soc_common_pcmcia_set_socket(
  428. struct pcmcia_socket *sock, socket_state_t *state)
  429. {
  430. struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
  431. debug(skt, 2, "mask: %s%s%s%s%s%s flags: %s%s%s%s%s%s Vcc %d Vpp %d irq %d\n",
  432. (state->csc_mask == 0) ? "<NONE> " : "",
  433. (state->csc_mask & SS_DETECT) ? "DETECT " : "",
  434. (state->csc_mask & SS_READY) ? "READY " : "",
  435. (state->csc_mask & SS_BATDEAD) ? "BATDEAD " : "",
  436. (state->csc_mask & SS_BATWARN) ? "BATWARN " : "",
  437. (state->csc_mask & SS_STSCHG) ? "STSCHG " : "",
  438. (state->flags == 0) ? "<NONE> " : "",
  439. (state->flags & SS_PWR_AUTO) ? "PWR_AUTO " : "",
  440. (state->flags & SS_IOCARD) ? "IOCARD " : "",
  441. (state->flags & SS_RESET) ? "RESET " : "",
  442. (state->flags & SS_SPKR_ENA) ? "SPKR_ENA " : "",
  443. (state->flags & SS_OUTPUT_ENA) ? "OUTPUT_ENA " : "",
  444. state->Vcc, state->Vpp, state->io_irq);
  445. return soc_common_pcmcia_config_skt(skt, state);
  446. }
  447. /*
  448. * Implements the set_io_map() operation for the in-kernel PCMCIA
  449. * service (formerly SS_SetIOMap in Card Services). We configure
  450. * the map speed as requested, but override the address ranges
  451. * supplied by Card Services.
  452. *
  453. * Returns: 0 on success, -1 on error
  454. */
  455. static int soc_common_pcmcia_set_io_map(
  456. struct pcmcia_socket *sock, struct pccard_io_map *map)
  457. {
  458. struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
  459. unsigned short speed = map->speed;
  460. debug(skt, 2, "map %u speed %u start 0x%08llx stop 0x%08llx\n",
  461. map->map, map->speed, (unsigned long long)map->start,
  462. (unsigned long long)map->stop);
  463. debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
  464. (map->flags == 0) ? "<NONE>" : "",
  465. (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
  466. (map->flags & MAP_16BIT) ? "16BIT " : "",
  467. (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
  468. (map->flags & MAP_0WS) ? "0WS " : "",
  469. (map->flags & MAP_WRPROT) ? "WRPROT " : "",
  470. (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "",
  471. (map->flags & MAP_PREFETCH) ? "PREFETCH " : "");
  472. if (map->map >= MAX_IO_WIN) {
  473. printk(KERN_ERR "%s(): map (%d) out of range\n", __func__,
  474. map->map);
  475. return -1;
  476. }
  477. if (map->flags & MAP_ACTIVE) {
  478. if (speed == 0)
  479. speed = SOC_PCMCIA_IO_ACCESS;
  480. } else {
  481. speed = 0;
  482. }
  483. skt->spd_io[map->map] = speed;
  484. skt->ops->set_timing(skt);
  485. if (map->stop == 1)
  486. map->stop = PAGE_SIZE-1;
  487. map->stop -= map->start;
  488. map->stop += skt->socket.io_offset;
  489. map->start = skt->socket.io_offset;
  490. return 0;
  491. }
  492. /*
  493. * Implements the set_mem_map() operation for the in-kernel PCMCIA
  494. * service (formerly SS_SetMemMap in Card Services). We configure
  495. * the map speed as requested, but override the address ranges
  496. * supplied by Card Services.
  497. *
  498. * Returns: 0 on success, -ERRNO on error
  499. */
  500. static int soc_common_pcmcia_set_mem_map(
  501. struct pcmcia_socket *sock, struct pccard_mem_map *map)
  502. {
  503. struct soc_pcmcia_socket *skt = to_soc_pcmcia_socket(sock);
  504. struct resource *res;
  505. unsigned short speed = map->speed;
  506. debug(skt, 2, "map %u speed %u card_start %08x\n",
  507. map->map, map->speed, map->card_start);
  508. debug(skt, 2, "flags: %s%s%s%s%s%s%s%s\n",
  509. (map->flags == 0) ? "<NONE>" : "",
  510. (map->flags & MAP_ACTIVE) ? "ACTIVE " : "",
  511. (map->flags & MAP_16BIT) ? "16BIT " : "",
  512. (map->flags & MAP_AUTOSZ) ? "AUTOSZ " : "",
  513. (map->flags & MAP_0WS) ? "0WS " : "",
  514. (map->flags & MAP_WRPROT) ? "WRPROT " : "",
  515. (map->flags & MAP_ATTRIB) ? "ATTRIB " : "",
  516. (map->flags & MAP_USE_WAIT) ? "USE_WAIT " : "");
  517. if (map->map >= MAX_WIN)
  518. return -EINVAL;
  519. if (map->flags & MAP_ACTIVE) {
  520. if (speed == 0)
  521. speed = 300;
  522. } else {
  523. speed = 0;
  524. }
  525. if (map->flags & MAP_ATTRIB) {
  526. res = &skt->res_attr;
  527. skt->spd_attr[map->map] = speed;
  528. skt->spd_mem[map->map] = 0;
  529. } else {
  530. res = &skt->res_mem;
  531. skt->spd_attr[map->map] = 0;
  532. skt->spd_mem[map->map] = speed;
  533. }
  534. skt->ops->set_timing(skt);
  535. map->static_start = res->start + map->card_start;
  536. return 0;
  537. }
  538. struct bittbl {
  539. unsigned int mask;
  540. const char *name;
  541. };
  542. static struct bittbl status_bits[] = {
  543. { SS_WRPROT, "SS_WRPROT" },
  544. { SS_BATDEAD, "SS_BATDEAD" },
  545. { SS_BATWARN, "SS_BATWARN" },
  546. { SS_READY, "SS_READY" },
  547. { SS_DETECT, "SS_DETECT" },
  548. { SS_POWERON, "SS_POWERON" },
  549. { SS_STSCHG, "SS_STSCHG" },
  550. { SS_3VCARD, "SS_3VCARD" },
  551. { SS_XVCARD, "SS_XVCARD" },
  552. };
  553. static struct bittbl conf_bits[] = {
  554. { SS_PWR_AUTO, "SS_PWR_AUTO" },
  555. { SS_IOCARD, "SS_IOCARD" },
  556. { SS_RESET, "SS_RESET" },
  557. { SS_DMA_MODE, "SS_DMA_MODE" },
  558. { SS_SPKR_ENA, "SS_SPKR_ENA" },
  559. { SS_OUTPUT_ENA, "SS_OUTPUT_ENA" },
  560. };
  561. static void dump_bits(char **p, const char *prefix,
  562. unsigned int val, struct bittbl *bits, int sz)
  563. {
  564. char *b = *p;
  565. int i;
  566. b += sprintf(b, "%-9s:", prefix);
  567. for (i = 0; i < sz; i++)
  568. if (val & bits[i].mask)
  569. b += sprintf(b, " %s", bits[i].name);
  570. *b++ = '\n';
  571. *p = b;
  572. }
  573. /*
  574. * Implements the /sys/class/pcmcia_socket/??/status file.
  575. *
  576. * Returns: the number of characters added to the buffer
  577. */
  578. static ssize_t show_status(
  579. struct device *dev, struct device_attribute *attr, char *buf)
  580. {
  581. struct soc_pcmcia_socket *skt =
  582. container_of(dev, struct soc_pcmcia_socket, socket.dev);
  583. char *p = buf;
  584. p += sprintf(p, "slot : %d\n", skt->nr);
  585. dump_bits(&p, "status", skt->status,
  586. status_bits, ARRAY_SIZE(status_bits));
  587. dump_bits(&p, "csc_mask", skt->cs_state.csc_mask,
  588. status_bits, ARRAY_SIZE(status_bits));
  589. dump_bits(&p, "cs_flags", skt->cs_state.flags,
  590. conf_bits, ARRAY_SIZE(conf_bits));
  591. p += sprintf(p, "Vcc : %d\n", skt->cs_state.Vcc);
  592. p += sprintf(p, "Vpp : %d\n", skt->cs_state.Vpp);
  593. p += sprintf(p, "IRQ : %d (%d)\n", skt->cs_state.io_irq,
  594. skt->socket.pci_irq);
  595. if (skt->ops->show_timing)
  596. p += skt->ops->show_timing(skt, p);
  597. return p-buf;
  598. }
  599. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  600. static struct pccard_operations soc_common_pcmcia_operations = {
  601. .init = soc_common_pcmcia_sock_init,
  602. .suspend = soc_common_pcmcia_suspend,
  603. .get_status = soc_common_pcmcia_get_status,
  604. .set_socket = soc_common_pcmcia_set_socket,
  605. .set_io_map = soc_common_pcmcia_set_io_map,
  606. .set_mem_map = soc_common_pcmcia_set_mem_map,
  607. };
  608. #ifdef CONFIG_CPU_FREQ
  609. static int soc_common_pcmcia_cpufreq_nb(struct notifier_block *nb,
  610. unsigned long val, void *data)
  611. {
  612. struct soc_pcmcia_socket *skt = container_of(nb, struct soc_pcmcia_socket, cpufreq_nb);
  613. struct cpufreq_freqs *freqs = data;
  614. return skt->ops->frequency_change(skt, val, freqs);
  615. }
  616. #endif
  617. void soc_pcmcia_init_one(struct soc_pcmcia_socket *skt,
  618. const struct pcmcia_low_level *ops, struct device *dev)
  619. {
  620. int i;
  621. skt->ops = ops;
  622. skt->socket.owner = ops->owner;
  623. skt->socket.dev.parent = dev;
  624. skt->socket.pci_irq = NO_IRQ;
  625. for (i = 0; i < ARRAY_SIZE(skt->stat); i++)
  626. skt->stat[i].gpio = -EINVAL;
  627. }
  628. EXPORT_SYMBOL(soc_pcmcia_init_one);
  629. void soc_pcmcia_remove_one(struct soc_pcmcia_socket *skt)
  630. {
  631. del_timer_sync(&skt->poll_timer);
  632. pcmcia_unregister_socket(&skt->socket);
  633. #ifdef CONFIG_CPU_FREQ
  634. if (skt->ops->frequency_change)
  635. cpufreq_unregister_notifier(&skt->cpufreq_nb,
  636. CPUFREQ_TRANSITION_NOTIFIER);
  637. #endif
  638. soc_pcmcia_hw_shutdown(skt);
  639. /* should not be required; violates some lowlevel drivers */
  640. soc_common_pcmcia_config_skt(skt, &dead_socket);
  641. iounmap(skt->virt_io);
  642. skt->virt_io = NULL;
  643. release_resource(&skt->res_attr);
  644. release_resource(&skt->res_mem);
  645. release_resource(&skt->res_io);
  646. release_resource(&skt->res_skt);
  647. }
  648. EXPORT_SYMBOL(soc_pcmcia_remove_one);
  649. int soc_pcmcia_add_one(struct soc_pcmcia_socket *skt)
  650. {
  651. int ret;
  652. skt->cs_state = dead_socket;
  653. timer_setup(&skt->poll_timer, soc_common_pcmcia_poll_event, 0);
  654. skt->poll_timer.expires = jiffies + SOC_PCMCIA_POLL_PERIOD;
  655. ret = request_resource(&iomem_resource, &skt->res_skt);
  656. if (ret)
  657. goto out_err_1;
  658. ret = request_resource(&skt->res_skt, &skt->res_io);
  659. if (ret)
  660. goto out_err_2;
  661. ret = request_resource(&skt->res_skt, &skt->res_mem);
  662. if (ret)
  663. goto out_err_3;
  664. ret = request_resource(&skt->res_skt, &skt->res_attr);
  665. if (ret)
  666. goto out_err_4;
  667. skt->virt_io = ioremap(skt->res_io.start, 0x10000);
  668. if (skt->virt_io == NULL) {
  669. ret = -ENOMEM;
  670. goto out_err_5;
  671. }
  672. /*
  673. * We initialize default socket timing here, because
  674. * we are not guaranteed to see a SetIOMap operation at
  675. * runtime.
  676. */
  677. skt->ops->set_timing(skt);
  678. ret = soc_pcmcia_hw_init(skt);
  679. if (ret)
  680. goto out_err_6;
  681. skt->socket.ops = &soc_common_pcmcia_operations;
  682. skt->socket.features = SS_CAP_STATIC_MAP|SS_CAP_PCCARD;
  683. skt->socket.resource_ops = &pccard_static_ops;
  684. skt->socket.irq_mask = 0;
  685. skt->socket.map_size = PAGE_SIZE;
  686. skt->socket.io_offset = (unsigned long)skt->virt_io;
  687. skt->status = soc_common_pcmcia_skt_state(skt);
  688. #ifdef CONFIG_CPU_FREQ
  689. if (skt->ops->frequency_change) {
  690. skt->cpufreq_nb.notifier_call = soc_common_pcmcia_cpufreq_nb;
  691. ret = cpufreq_register_notifier(&skt->cpufreq_nb,
  692. CPUFREQ_TRANSITION_NOTIFIER);
  693. if (ret < 0)
  694. dev_err(skt->socket.dev.parent,
  695. "unable to register CPU frequency change notifier for PCMCIA (%d)\n",
  696. ret);
  697. }
  698. #endif
  699. ret = pcmcia_register_socket(&skt->socket);
  700. if (ret)
  701. goto out_err_7;
  702. ret = device_create_file(&skt->socket.dev, &dev_attr_status);
  703. if (ret)
  704. goto out_err_8;
  705. return ret;
  706. out_err_8:
  707. del_timer_sync(&skt->poll_timer);
  708. pcmcia_unregister_socket(&skt->socket);
  709. out_err_7:
  710. soc_pcmcia_hw_shutdown(skt);
  711. out_err_6:
  712. iounmap(skt->virt_io);
  713. out_err_5:
  714. release_resource(&skt->res_attr);
  715. out_err_4:
  716. release_resource(&skt->res_mem);
  717. out_err_3:
  718. release_resource(&skt->res_io);
  719. out_err_2:
  720. release_resource(&skt->res_skt);
  721. out_err_1:
  722. return ret;
  723. }
  724. EXPORT_SYMBOL(soc_pcmcia_add_one);
  725. MODULE_AUTHOR("John Dorsey <john+@cs.cmu.edu>");
  726. MODULE_DESCRIPTION("Linux PCMCIA Card Services: Common SoC support");
  727. MODULE_LICENSE("Dual MPL/GPL");