spi-fsl-spi.c 22 KB

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