spi-fsl-spi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Freescale SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. * Copyright 2010 Freescale Semiconductor, Inc.
  8. *
  9. * CPM SPI and QE buffer descriptors mode support:
  10. * Copyright (c) 2009 MontaVista Software, Inc.
  11. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/delay.h>
  23. #include <linux/irq.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/spi_bitbang.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/fsl_devices.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/mm.h>
  30. #include <linux/mutex.h>
  31. #include <linux/of.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/gpio.h>
  36. #include <linux/of_gpio.h>
  37. #include "spi-fsl-lib.h"
  38. #include "spi-fsl-cpm.h"
  39. #include "spi-fsl-spi.h"
  40. #define TYPE_FSL 0
  41. struct fsl_spi_match_data {
  42. int type;
  43. };
  44. static struct fsl_spi_match_data of_fsl_spi_fsl_config = {
  45. .type = TYPE_FSL,
  46. };
  47. static struct of_device_id of_fsl_spi_match[] = {
  48. {
  49. .compatible = "fsl,spi",
  50. .data = &of_fsl_spi_fsl_config,
  51. },
  52. {}
  53. };
  54. MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
  55. static int fsl_spi_get_type(struct device *dev)
  56. {
  57. const struct of_device_id *match;
  58. if (dev->of_node) {
  59. match = of_match_node(of_fsl_spi_match, dev->of_node);
  60. if (match && match->data)
  61. return ((struct fsl_spi_match_data *)match->data)->type;
  62. }
  63. return TYPE_FSL;
  64. }
  65. static void fsl_spi_change_mode(struct spi_device *spi)
  66. {
  67. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  68. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  69. struct fsl_spi_reg *reg_base = mspi->reg_base;
  70. __be32 __iomem *mode = &reg_base->mode;
  71. unsigned long flags;
  72. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  73. return;
  74. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  75. local_irq_save(flags);
  76. /* Turn off SPI unit prior changing mode */
  77. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  78. /* When in CPM mode, we need to reinit tx and rx. */
  79. if (mspi->flags & SPI_CPM_MODE) {
  80. fsl_spi_cpm_reinit_txrx(mspi);
  81. }
  82. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  83. local_irq_restore(flags);
  84. }
  85. static void fsl_spi_chipselect(struct spi_device *spi, int value)
  86. {
  87. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  88. struct fsl_spi_platform_data *pdata;
  89. bool pol = spi->mode & SPI_CS_HIGH;
  90. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  91. pdata = spi->dev.parent->parent->platform_data;
  92. if (value == BITBANG_CS_INACTIVE) {
  93. if (pdata->cs_control)
  94. pdata->cs_control(spi, !pol);
  95. }
  96. if (value == BITBANG_CS_ACTIVE) {
  97. mpc8xxx_spi->rx_shift = cs->rx_shift;
  98. mpc8xxx_spi->tx_shift = cs->tx_shift;
  99. mpc8xxx_spi->get_rx = cs->get_rx;
  100. mpc8xxx_spi->get_tx = cs->get_tx;
  101. fsl_spi_change_mode(spi);
  102. if (pdata->cs_control)
  103. pdata->cs_control(spi, pol);
  104. }
  105. }
  106. static void fsl_spi_qe_cpu_set_shifts(u32 *rx_shift, u32 *tx_shift,
  107. int bits_per_word, int msb_first)
  108. {
  109. *rx_shift = 0;
  110. *tx_shift = 0;
  111. if (msb_first) {
  112. if (bits_per_word <= 8) {
  113. *rx_shift = 16;
  114. *tx_shift = 24;
  115. } else if (bits_per_word <= 16) {
  116. *rx_shift = 16;
  117. *tx_shift = 16;
  118. }
  119. } else {
  120. if (bits_per_word <= 8)
  121. *rx_shift = 8;
  122. }
  123. }
  124. static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
  125. struct spi_device *spi,
  126. struct mpc8xxx_spi *mpc8xxx_spi,
  127. int bits_per_word)
  128. {
  129. cs->rx_shift = 0;
  130. cs->tx_shift = 0;
  131. if (bits_per_word <= 8) {
  132. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  133. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  134. } else if (bits_per_word <= 16) {
  135. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  136. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  137. } else if (bits_per_word <= 32) {
  138. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  139. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  140. } else
  141. return -EINVAL;
  142. if (mpc8xxx_spi->set_shifts)
  143. mpc8xxx_spi->set_shifts(&cs->rx_shift, &cs->tx_shift,
  144. bits_per_word,
  145. !(spi->mode & SPI_LSB_FIRST));
  146. mpc8xxx_spi->rx_shift = cs->rx_shift;
  147. mpc8xxx_spi->tx_shift = cs->tx_shift;
  148. mpc8xxx_spi->get_rx = cs->get_rx;
  149. mpc8xxx_spi->get_tx = cs->get_tx;
  150. return bits_per_word;
  151. }
  152. static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
  153. struct spi_device *spi,
  154. int bits_per_word)
  155. {
  156. /* QE uses Little Endian for words > 8
  157. * so transform all words > 8 into 8 bits
  158. * Unfortnatly that doesn't work for LSB so
  159. * reject these for now */
  160. /* Note: 32 bits word, LSB works iff
  161. * tfcr/rfcr is set to CPMFCR_GBL */
  162. if (spi->mode & SPI_LSB_FIRST &&
  163. bits_per_word > 8)
  164. return -EINVAL;
  165. if (bits_per_word > 8)
  166. return 8; /* pretend its 8 bits */
  167. return bits_per_word;
  168. }
  169. static int fsl_spi_setup_transfer(struct spi_device *spi,
  170. struct spi_transfer *t)
  171. {
  172. struct mpc8xxx_spi *mpc8xxx_spi;
  173. int bits_per_word = 0;
  174. u8 pm;
  175. u32 hz = 0;
  176. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  177. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  178. if (t) {
  179. bits_per_word = t->bits_per_word;
  180. hz = t->speed_hz;
  181. }
  182. /* spi_transfer level calls that work per-word */
  183. if (!bits_per_word)
  184. bits_per_word = spi->bits_per_word;
  185. /* Make sure its a bit width we support [4..16, 32] */
  186. if ((bits_per_word < 4)
  187. || ((bits_per_word > 16) && (bits_per_word != 32)))
  188. return -EINVAL;
  189. if (!hz)
  190. hz = spi->max_speed_hz;
  191. if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
  192. bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
  193. mpc8xxx_spi,
  194. bits_per_word);
  195. else if (mpc8xxx_spi->flags & SPI_QE)
  196. bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
  197. bits_per_word);
  198. if (bits_per_word < 0)
  199. return bits_per_word;
  200. if (bits_per_word == 32)
  201. bits_per_word = 0;
  202. else
  203. bits_per_word = bits_per_word - 1;
  204. /* mask out bits we are going to set */
  205. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  206. | SPMODE_PM(0xF));
  207. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  208. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  209. cs->hw_mode |= SPMODE_DIV16;
  210. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
  211. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  212. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  213. hz, mpc8xxx_spi->spibrg / 1024);
  214. if (pm > 16)
  215. pm = 16;
  216. } else {
  217. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
  218. }
  219. if (pm)
  220. pm--;
  221. cs->hw_mode |= SPMODE_PM(pm);
  222. fsl_spi_change_mode(spi);
  223. return 0;
  224. }
  225. static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  226. struct spi_transfer *t, unsigned int len)
  227. {
  228. u32 word;
  229. struct fsl_spi_reg *reg_base = mspi->reg_base;
  230. mspi->count = len;
  231. /* enable rx ints */
  232. mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
  233. /* transmit word */
  234. word = mspi->get_tx(mspi);
  235. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  236. return 0;
  237. }
  238. static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  239. bool is_dma_mapped)
  240. {
  241. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  242. struct fsl_spi_reg *reg_base;
  243. unsigned int len = t->len;
  244. u8 bits_per_word;
  245. int ret;
  246. reg_base = mpc8xxx_spi->reg_base;
  247. bits_per_word = spi->bits_per_word;
  248. if (t->bits_per_word)
  249. bits_per_word = t->bits_per_word;
  250. if (bits_per_word > 8) {
  251. /* invalid length? */
  252. if (len & 1)
  253. return -EINVAL;
  254. len /= 2;
  255. }
  256. if (bits_per_word > 16) {
  257. /* invalid length? */
  258. if (len & 1)
  259. return -EINVAL;
  260. len /= 2;
  261. }
  262. mpc8xxx_spi->tx = t->tx_buf;
  263. mpc8xxx_spi->rx = t->rx_buf;
  264. INIT_COMPLETION(mpc8xxx_spi->done);
  265. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  266. ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  267. else
  268. ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
  269. if (ret)
  270. return ret;
  271. wait_for_completion(&mpc8xxx_spi->done);
  272. /* disable rx ints */
  273. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  274. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  275. fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
  276. return mpc8xxx_spi->count;
  277. }
  278. static void fsl_spi_do_one_msg(struct spi_message *m)
  279. {
  280. struct spi_device *spi = m->spi;
  281. struct spi_transfer *t;
  282. unsigned int cs_change;
  283. const int nsecs = 50;
  284. int status;
  285. cs_change = 1;
  286. status = 0;
  287. list_for_each_entry(t, &m->transfers, transfer_list) {
  288. if (t->bits_per_word || t->speed_hz) {
  289. /* Don't allow changes if CS is active */
  290. status = -EINVAL;
  291. if (cs_change)
  292. status = fsl_spi_setup_transfer(spi, t);
  293. if (status < 0)
  294. break;
  295. }
  296. if (cs_change) {
  297. fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  298. ndelay(nsecs);
  299. }
  300. cs_change = t->cs_change;
  301. if (t->len)
  302. status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
  303. if (status) {
  304. status = -EMSGSIZE;
  305. break;
  306. }
  307. m->actual_length += t->len;
  308. if (t->delay_usecs)
  309. udelay(t->delay_usecs);
  310. if (cs_change) {
  311. ndelay(nsecs);
  312. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  313. ndelay(nsecs);
  314. }
  315. }
  316. m->status = status;
  317. m->complete(m->context);
  318. if (status || !cs_change) {
  319. ndelay(nsecs);
  320. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  321. }
  322. fsl_spi_setup_transfer(spi, NULL);
  323. }
  324. static int fsl_spi_setup(struct spi_device *spi)
  325. {
  326. struct mpc8xxx_spi *mpc8xxx_spi;
  327. struct fsl_spi_reg *reg_base;
  328. int retval;
  329. u32 hw_mode;
  330. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  331. if (!spi->max_speed_hz)
  332. return -EINVAL;
  333. if (!cs) {
  334. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  335. if (!cs)
  336. return -ENOMEM;
  337. spi->controller_state = cs;
  338. }
  339. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  340. reg_base = mpc8xxx_spi->reg_base;
  341. hw_mode = cs->hw_mode; /* Save original settings */
  342. cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
  343. /* mask out bits we are going to set */
  344. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  345. | SPMODE_REV | SPMODE_LOOP);
  346. if (spi->mode & SPI_CPHA)
  347. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  348. if (spi->mode & SPI_CPOL)
  349. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  350. if (!(spi->mode & SPI_LSB_FIRST))
  351. cs->hw_mode |= SPMODE_REV;
  352. if (spi->mode & SPI_LOOP)
  353. cs->hw_mode |= SPMODE_LOOP;
  354. retval = fsl_spi_setup_transfer(spi, NULL);
  355. if (retval < 0) {
  356. cs->hw_mode = hw_mode; /* Restore settings */
  357. return retval;
  358. }
  359. /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
  360. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  361. return 0;
  362. }
  363. static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  364. {
  365. struct fsl_spi_reg *reg_base = mspi->reg_base;
  366. /* We need handle RX first */
  367. if (events & SPIE_NE) {
  368. u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
  369. if (mspi->rx)
  370. mspi->get_rx(rx_data, mspi);
  371. }
  372. if ((events & SPIE_NF) == 0)
  373. /* spin until TX is done */
  374. while (((events =
  375. mpc8xxx_spi_read_reg(&reg_base->event)) &
  376. SPIE_NF) == 0)
  377. cpu_relax();
  378. /* Clear the events */
  379. mpc8xxx_spi_write_reg(&reg_base->event, events);
  380. mspi->count -= 1;
  381. if (mspi->count) {
  382. u32 word = mspi->get_tx(mspi);
  383. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  384. } else {
  385. complete(&mspi->done);
  386. }
  387. }
  388. static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
  389. {
  390. struct mpc8xxx_spi *mspi = context_data;
  391. irqreturn_t ret = IRQ_NONE;
  392. u32 events;
  393. struct fsl_spi_reg *reg_base = mspi->reg_base;
  394. /* Get interrupt events(tx/rx) */
  395. events = mpc8xxx_spi_read_reg(&reg_base->event);
  396. if (events)
  397. ret = IRQ_HANDLED;
  398. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  399. if (mspi->flags & SPI_CPM_MODE)
  400. fsl_spi_cpm_irq(mspi, events);
  401. else
  402. fsl_spi_cpu_irq(mspi, events);
  403. return ret;
  404. }
  405. static void fsl_spi_remove(struct mpc8xxx_spi *mspi)
  406. {
  407. iounmap(mspi->reg_base);
  408. fsl_spi_cpm_free(mspi);
  409. }
  410. static struct spi_master * fsl_spi_probe(struct device *dev,
  411. struct resource *mem, unsigned int irq)
  412. {
  413. struct fsl_spi_platform_data *pdata = dev->platform_data;
  414. struct spi_master *master;
  415. struct mpc8xxx_spi *mpc8xxx_spi;
  416. struct fsl_spi_reg *reg_base;
  417. u32 regval;
  418. int ret = 0;
  419. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  420. if (master == NULL) {
  421. ret = -ENOMEM;
  422. goto err;
  423. }
  424. dev_set_drvdata(dev, master);
  425. ret = mpc8xxx_spi_probe(dev, mem, irq);
  426. if (ret)
  427. goto err_probe;
  428. master->setup = fsl_spi_setup;
  429. mpc8xxx_spi = spi_master_get_devdata(master);
  430. mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg;
  431. mpc8xxx_spi->spi_remove = fsl_spi_remove;
  432. mpc8xxx_spi->type = fsl_spi_get_type(dev);
  433. ret = fsl_spi_cpm_init(mpc8xxx_spi);
  434. if (ret)
  435. goto err_cpm_init;
  436. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  437. mpc8xxx_spi->set_shifts = fsl_spi_qe_cpu_set_shifts;
  438. if (mpc8xxx_spi->set_shifts)
  439. /* 8 bits per word and MSB first */
  440. mpc8xxx_spi->set_shifts(&mpc8xxx_spi->rx_shift,
  441. &mpc8xxx_spi->tx_shift, 8, 1);
  442. mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem));
  443. if (mpc8xxx_spi->reg_base == NULL) {
  444. ret = -ENOMEM;
  445. goto err_ioremap;
  446. }
  447. /* Register for SPI Interrupt */
  448. ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq,
  449. 0, "fsl_spi", mpc8xxx_spi);
  450. if (ret != 0)
  451. goto free_irq;
  452. reg_base = mpc8xxx_spi->reg_base;
  453. /* SPI controller initializations */
  454. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  455. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  456. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  457. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  458. /* Enable SPI interface */
  459. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  460. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  461. regval |= SPMODE_OP;
  462. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  463. ret = spi_register_master(master);
  464. if (ret < 0)
  465. goto unreg_master;
  466. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
  467. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  468. return master;
  469. unreg_master:
  470. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  471. free_irq:
  472. iounmap(mpc8xxx_spi->reg_base);
  473. err_ioremap:
  474. fsl_spi_cpm_free(mpc8xxx_spi);
  475. err_cpm_init:
  476. err_probe:
  477. spi_master_put(master);
  478. err:
  479. return ERR_PTR(ret);
  480. }
  481. static void fsl_spi_cs_control(struct spi_device *spi, bool on)
  482. {
  483. struct device *dev = spi->dev.parent->parent;
  484. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  485. u16 cs = spi->chip_select;
  486. int gpio = pinfo->gpios[cs];
  487. bool alow = pinfo->alow_flags[cs];
  488. gpio_set_value(gpio, on ^ alow);
  489. }
  490. static int of_fsl_spi_get_chipselects(struct device *dev)
  491. {
  492. struct device_node *np = dev->of_node;
  493. struct fsl_spi_platform_data *pdata = dev->platform_data;
  494. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  495. int ngpios;
  496. int i = 0;
  497. int ret;
  498. ngpios = of_gpio_count(np);
  499. if (ngpios <= 0) {
  500. /*
  501. * SPI w/o chip-select line. One SPI device is still permitted
  502. * though.
  503. */
  504. pdata->max_chipselect = 1;
  505. return 0;
  506. }
  507. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  508. if (!pinfo->gpios)
  509. return -ENOMEM;
  510. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  511. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  512. GFP_KERNEL);
  513. if (!pinfo->alow_flags) {
  514. ret = -ENOMEM;
  515. goto err_alloc_flags;
  516. }
  517. for (; i < ngpios; i++) {
  518. int gpio;
  519. enum of_gpio_flags flags;
  520. gpio = of_get_gpio_flags(np, i, &flags);
  521. if (!gpio_is_valid(gpio)) {
  522. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  523. ret = gpio;
  524. goto err_loop;
  525. }
  526. ret = gpio_request(gpio, dev_name(dev));
  527. if (ret) {
  528. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  529. goto err_loop;
  530. }
  531. pinfo->gpios[i] = gpio;
  532. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  533. ret = gpio_direction_output(pinfo->gpios[i],
  534. pinfo->alow_flags[i]);
  535. if (ret) {
  536. dev_err(dev, "can't set output direction for gpio "
  537. "#%d: %d\n", i, ret);
  538. goto err_loop;
  539. }
  540. }
  541. pdata->max_chipselect = ngpios;
  542. pdata->cs_control = fsl_spi_cs_control;
  543. return 0;
  544. err_loop:
  545. while (i >= 0) {
  546. if (gpio_is_valid(pinfo->gpios[i]))
  547. gpio_free(pinfo->gpios[i]);
  548. i--;
  549. }
  550. kfree(pinfo->alow_flags);
  551. pinfo->alow_flags = NULL;
  552. err_alloc_flags:
  553. kfree(pinfo->gpios);
  554. pinfo->gpios = NULL;
  555. return ret;
  556. }
  557. static int of_fsl_spi_free_chipselects(struct device *dev)
  558. {
  559. struct fsl_spi_platform_data *pdata = dev->platform_data;
  560. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  561. int i;
  562. if (!pinfo->gpios)
  563. return 0;
  564. for (i = 0; i < pdata->max_chipselect; i++) {
  565. if (gpio_is_valid(pinfo->gpios[i]))
  566. gpio_free(pinfo->gpios[i]);
  567. }
  568. kfree(pinfo->gpios);
  569. kfree(pinfo->alow_flags);
  570. return 0;
  571. }
  572. static int of_fsl_spi_probe(struct platform_device *ofdev)
  573. {
  574. struct device *dev = &ofdev->dev;
  575. struct device_node *np = ofdev->dev.of_node;
  576. struct spi_master *master;
  577. struct resource mem;
  578. int irq;
  579. int ret = -ENOMEM;
  580. ret = of_mpc8xxx_spi_probe(ofdev);
  581. if (ret)
  582. return ret;
  583. ret = of_fsl_spi_get_chipselects(dev);
  584. if (ret)
  585. goto err;
  586. ret = of_address_to_resource(np, 0, &mem);
  587. if (ret)
  588. goto err;
  589. irq = irq_of_parse_and_map(np, 0);
  590. if (!irq) {
  591. ret = -EINVAL;
  592. goto err;
  593. }
  594. master = fsl_spi_probe(dev, &mem, irq);
  595. if (IS_ERR(master)) {
  596. ret = PTR_ERR(master);
  597. goto err;
  598. }
  599. return 0;
  600. err:
  601. of_fsl_spi_free_chipselects(dev);
  602. return ret;
  603. }
  604. static int of_fsl_spi_remove(struct platform_device *ofdev)
  605. {
  606. int ret;
  607. ret = mpc8xxx_spi_remove(&ofdev->dev);
  608. if (ret)
  609. return ret;
  610. of_fsl_spi_free_chipselects(&ofdev->dev);
  611. return 0;
  612. }
  613. static struct platform_driver of_fsl_spi_driver = {
  614. .driver = {
  615. .name = "fsl_spi",
  616. .owner = THIS_MODULE,
  617. .of_match_table = of_fsl_spi_match,
  618. },
  619. .probe = of_fsl_spi_probe,
  620. .remove = of_fsl_spi_remove,
  621. };
  622. #ifdef CONFIG_MPC832x_RDB
  623. /*
  624. * XXX XXX XXX
  625. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  626. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  627. * tree can work with OpenFirmware driver. But for now we support old trees
  628. * as well.
  629. */
  630. static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  631. {
  632. struct resource *mem;
  633. int irq;
  634. struct spi_master *master;
  635. if (!pdev->dev.platform_data)
  636. return -EINVAL;
  637. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  638. if (!mem)
  639. return -EINVAL;
  640. irq = platform_get_irq(pdev, 0);
  641. if (irq <= 0)
  642. return -EINVAL;
  643. master = fsl_spi_probe(&pdev->dev, mem, irq);
  644. return PTR_RET(master);
  645. }
  646. static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  647. {
  648. return mpc8xxx_spi_remove(&pdev->dev);
  649. }
  650. MODULE_ALIAS("platform:mpc8xxx_spi");
  651. static struct platform_driver mpc8xxx_spi_driver = {
  652. .probe = plat_mpc8xxx_spi_probe,
  653. .remove = plat_mpc8xxx_spi_remove,
  654. .driver = {
  655. .name = "mpc8xxx_spi",
  656. .owner = THIS_MODULE,
  657. },
  658. };
  659. static bool legacy_driver_failed;
  660. static void __init legacy_driver_register(void)
  661. {
  662. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  663. }
  664. static void __exit legacy_driver_unregister(void)
  665. {
  666. if (legacy_driver_failed)
  667. return;
  668. platform_driver_unregister(&mpc8xxx_spi_driver);
  669. }
  670. #else
  671. static void __init legacy_driver_register(void) {}
  672. static void __exit legacy_driver_unregister(void) {}
  673. #endif /* CONFIG_MPC832x_RDB */
  674. static int __init fsl_spi_init(void)
  675. {
  676. legacy_driver_register();
  677. return platform_driver_register(&of_fsl_spi_driver);
  678. }
  679. module_init(fsl_spi_init);
  680. static void __exit fsl_spi_exit(void)
  681. {
  682. platform_driver_unregister(&of_fsl_spi_driver);
  683. legacy_driver_unregister();
  684. }
  685. module_exit(fsl_spi_exit);
  686. MODULE_AUTHOR("Kumar Gala");
  687. MODULE_DESCRIPTION("Simple Freescale SPI Driver");
  688. MODULE_LICENSE("GPL");