spi-pxa2xx.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. /*
  2. * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
  3. * Copyright (C) 2013, Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/bitops.h>
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/device.h>
  19. #include <linux/ioport.h>
  20. #include <linux/errno.h>
  21. #include <linux/err.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/kernel.h>
  24. #include <linux/pci.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spi/pxa2xx_spi.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/delay.h>
  29. #include <linux/gpio.h>
  30. #include <linux/gpio/consumer.h>
  31. #include <linux/slab.h>
  32. #include <linux/clk.h>
  33. #include <linux/pm_runtime.h>
  34. #include <linux/acpi.h>
  35. #include "spi-pxa2xx.h"
  36. MODULE_AUTHOR("Stephen Street");
  37. MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
  38. MODULE_LICENSE("GPL");
  39. MODULE_ALIAS("platform:pxa2xx-spi");
  40. #define TIMOUT_DFLT 1000
  41. /*
  42. * for testing SSCR1 changes that require SSP restart, basically
  43. * everything except the service and interrupt enables, the pxa270 developer
  44. * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
  45. * list, but the PXA255 dev man says all bits without really meaning the
  46. * service and interrupt enables
  47. */
  48. #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  49. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  50. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  51. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  52. | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
  53. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  54. #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
  55. | QUARK_X1000_SSCR1_EFWR \
  56. | QUARK_X1000_SSCR1_RFT \
  57. | QUARK_X1000_SSCR1_TFT \
  58. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  59. #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
  60. | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
  61. | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
  62. | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
  63. | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
  64. | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
  65. #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
  66. #define LPSS_CS_CONTROL_SW_MODE BIT(0)
  67. #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
  68. #define LPSS_CAPS_CS_EN_SHIFT 9
  69. #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
  70. #define LPSS_PRIV_CLOCK_GATE 0x38
  71. #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
  72. #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
  73. struct lpss_config {
  74. /* LPSS offset from drv_data->ioaddr */
  75. unsigned offset;
  76. /* Register offsets from drv_data->lpss_base or -1 */
  77. int reg_general;
  78. int reg_ssp;
  79. int reg_cs_ctrl;
  80. int reg_capabilities;
  81. /* FIFO thresholds */
  82. u32 rx_threshold;
  83. u32 tx_threshold_lo;
  84. u32 tx_threshold_hi;
  85. /* Chip select control */
  86. unsigned cs_sel_shift;
  87. unsigned cs_sel_mask;
  88. unsigned cs_num;
  89. /* Quirks */
  90. unsigned cs_clk_stays_gated : 1;
  91. };
  92. /* Keep these sorted with enum pxa_ssp_type */
  93. static const struct lpss_config lpss_platforms[] = {
  94. { /* LPSS_LPT_SSP */
  95. .offset = 0x800,
  96. .reg_general = 0x08,
  97. .reg_ssp = 0x0c,
  98. .reg_cs_ctrl = 0x18,
  99. .reg_capabilities = -1,
  100. .rx_threshold = 64,
  101. .tx_threshold_lo = 160,
  102. .tx_threshold_hi = 224,
  103. },
  104. { /* LPSS_BYT_SSP */
  105. .offset = 0x400,
  106. .reg_general = 0x08,
  107. .reg_ssp = 0x0c,
  108. .reg_cs_ctrl = 0x18,
  109. .reg_capabilities = -1,
  110. .rx_threshold = 64,
  111. .tx_threshold_lo = 160,
  112. .tx_threshold_hi = 224,
  113. },
  114. { /* LPSS_BSW_SSP */
  115. .offset = 0x400,
  116. .reg_general = 0x08,
  117. .reg_ssp = 0x0c,
  118. .reg_cs_ctrl = 0x18,
  119. .reg_capabilities = -1,
  120. .rx_threshold = 64,
  121. .tx_threshold_lo = 160,
  122. .tx_threshold_hi = 224,
  123. .cs_sel_shift = 2,
  124. .cs_sel_mask = 1 << 2,
  125. .cs_num = 2,
  126. },
  127. { /* LPSS_SPT_SSP */
  128. .offset = 0x200,
  129. .reg_general = -1,
  130. .reg_ssp = 0x20,
  131. .reg_cs_ctrl = 0x24,
  132. .reg_capabilities = -1,
  133. .rx_threshold = 1,
  134. .tx_threshold_lo = 32,
  135. .tx_threshold_hi = 56,
  136. },
  137. { /* LPSS_BXT_SSP */
  138. .offset = 0x200,
  139. .reg_general = -1,
  140. .reg_ssp = 0x20,
  141. .reg_cs_ctrl = 0x24,
  142. .reg_capabilities = 0xfc,
  143. .rx_threshold = 1,
  144. .tx_threshold_lo = 16,
  145. .tx_threshold_hi = 48,
  146. .cs_sel_shift = 8,
  147. .cs_sel_mask = 3 << 8,
  148. },
  149. { /* LPSS_CNL_SSP */
  150. .offset = 0x200,
  151. .reg_general = -1,
  152. .reg_ssp = 0x20,
  153. .reg_cs_ctrl = 0x24,
  154. .reg_capabilities = 0xfc,
  155. .rx_threshold = 1,
  156. .tx_threshold_lo = 32,
  157. .tx_threshold_hi = 56,
  158. .cs_sel_shift = 8,
  159. .cs_sel_mask = 3 << 8,
  160. .cs_clk_stays_gated = true,
  161. },
  162. };
  163. static inline const struct lpss_config
  164. *lpss_get_config(const struct driver_data *drv_data)
  165. {
  166. return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
  167. }
  168. static bool is_lpss_ssp(const struct driver_data *drv_data)
  169. {
  170. switch (drv_data->ssp_type) {
  171. case LPSS_LPT_SSP:
  172. case LPSS_BYT_SSP:
  173. case LPSS_BSW_SSP:
  174. case LPSS_SPT_SSP:
  175. case LPSS_BXT_SSP:
  176. case LPSS_CNL_SSP:
  177. return true;
  178. default:
  179. return false;
  180. }
  181. }
  182. static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
  183. {
  184. return drv_data->ssp_type == QUARK_X1000_SSP;
  185. }
  186. static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
  187. {
  188. switch (drv_data->ssp_type) {
  189. case QUARK_X1000_SSP:
  190. return QUARK_X1000_SSCR1_CHANGE_MASK;
  191. case CE4100_SSP:
  192. return CE4100_SSCR1_CHANGE_MASK;
  193. default:
  194. return SSCR1_CHANGE_MASK;
  195. }
  196. }
  197. static u32
  198. pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
  199. {
  200. switch (drv_data->ssp_type) {
  201. case QUARK_X1000_SSP:
  202. return RX_THRESH_QUARK_X1000_DFLT;
  203. case CE4100_SSP:
  204. return RX_THRESH_CE4100_DFLT;
  205. default:
  206. return RX_THRESH_DFLT;
  207. }
  208. }
  209. static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
  210. {
  211. u32 mask;
  212. switch (drv_data->ssp_type) {
  213. case QUARK_X1000_SSP:
  214. mask = QUARK_X1000_SSSR_TFL_MASK;
  215. break;
  216. case CE4100_SSP:
  217. mask = CE4100_SSSR_TFL_MASK;
  218. break;
  219. default:
  220. mask = SSSR_TFL_MASK;
  221. break;
  222. }
  223. return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
  224. }
  225. static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
  226. u32 *sccr1_reg)
  227. {
  228. u32 mask;
  229. switch (drv_data->ssp_type) {
  230. case QUARK_X1000_SSP:
  231. mask = QUARK_X1000_SSCR1_RFT;
  232. break;
  233. case CE4100_SSP:
  234. mask = CE4100_SSCR1_RFT;
  235. break;
  236. default:
  237. mask = SSCR1_RFT;
  238. break;
  239. }
  240. *sccr1_reg &= ~mask;
  241. }
  242. static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
  243. u32 *sccr1_reg, u32 threshold)
  244. {
  245. switch (drv_data->ssp_type) {
  246. case QUARK_X1000_SSP:
  247. *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
  248. break;
  249. case CE4100_SSP:
  250. *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
  251. break;
  252. default:
  253. *sccr1_reg |= SSCR1_RxTresh(threshold);
  254. break;
  255. }
  256. }
  257. static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
  258. u32 clk_div, u8 bits)
  259. {
  260. switch (drv_data->ssp_type) {
  261. case QUARK_X1000_SSP:
  262. return clk_div
  263. | QUARK_X1000_SSCR0_Motorola
  264. | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
  265. | SSCR0_SSE;
  266. default:
  267. return clk_div
  268. | SSCR0_Motorola
  269. | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
  270. | SSCR0_SSE
  271. | (bits > 16 ? SSCR0_EDSS : 0);
  272. }
  273. }
  274. /*
  275. * Read and write LPSS SSP private registers. Caller must first check that
  276. * is_lpss_ssp() returns true before these can be called.
  277. */
  278. static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
  279. {
  280. WARN_ON(!drv_data->lpss_base);
  281. return readl(drv_data->lpss_base + offset);
  282. }
  283. static void __lpss_ssp_write_priv(struct driver_data *drv_data,
  284. unsigned offset, u32 value)
  285. {
  286. WARN_ON(!drv_data->lpss_base);
  287. writel(value, drv_data->lpss_base + offset);
  288. }
  289. /*
  290. * lpss_ssp_setup - perform LPSS SSP specific setup
  291. * @drv_data: pointer to the driver private data
  292. *
  293. * Perform LPSS SSP specific setup. This function must be called first if
  294. * one is going to use LPSS SSP private registers.
  295. */
  296. static void lpss_ssp_setup(struct driver_data *drv_data)
  297. {
  298. const struct lpss_config *config;
  299. u32 value;
  300. config = lpss_get_config(drv_data);
  301. drv_data->lpss_base = drv_data->ioaddr + config->offset;
  302. /* Enable software chip select control */
  303. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  304. value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
  305. value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
  306. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  307. /* Enable multiblock DMA transfers */
  308. if (drv_data->master_info->enable_dma) {
  309. __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
  310. if (config->reg_general >= 0) {
  311. value = __lpss_ssp_read_priv(drv_data,
  312. config->reg_general);
  313. value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
  314. __lpss_ssp_write_priv(drv_data,
  315. config->reg_general, value);
  316. }
  317. }
  318. }
  319. static void lpss_ssp_select_cs(struct spi_device *spi,
  320. const struct lpss_config *config)
  321. {
  322. struct driver_data *drv_data =
  323. spi_controller_get_devdata(spi->controller);
  324. u32 value, cs;
  325. if (!config->cs_sel_mask)
  326. return;
  327. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  328. cs = spi->chip_select;
  329. cs <<= config->cs_sel_shift;
  330. if (cs != (value & config->cs_sel_mask)) {
  331. /*
  332. * When switching another chip select output active the
  333. * output must be selected first and wait 2 ssp_clk cycles
  334. * before changing state to active. Otherwise a short
  335. * glitch will occur on the previous chip select since
  336. * output select is latched but state control is not.
  337. */
  338. value &= ~config->cs_sel_mask;
  339. value |= cs;
  340. __lpss_ssp_write_priv(drv_data,
  341. config->reg_cs_ctrl, value);
  342. ndelay(1000000000 /
  343. (drv_data->master->max_speed_hz / 2));
  344. }
  345. }
  346. static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
  347. {
  348. struct driver_data *drv_data =
  349. spi_controller_get_devdata(spi->controller);
  350. const struct lpss_config *config;
  351. u32 value;
  352. config = lpss_get_config(drv_data);
  353. if (enable)
  354. lpss_ssp_select_cs(spi, config);
  355. value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
  356. if (enable)
  357. value &= ~LPSS_CS_CONTROL_CS_HIGH;
  358. else
  359. value |= LPSS_CS_CONTROL_CS_HIGH;
  360. __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
  361. if (config->cs_clk_stays_gated) {
  362. u32 clkgate;
  363. /*
  364. * Changing CS alone when dynamic clock gating is on won't
  365. * actually flip CS at that time. This ruins SPI transfers
  366. * that specify delays, or have no data. Toggle the clock mode
  367. * to force on briefly to poke the CS pin to move.
  368. */
  369. clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
  370. value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
  371. LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
  372. __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
  373. __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
  374. }
  375. }
  376. static void cs_assert(struct spi_device *spi)
  377. {
  378. struct chip_data *chip = spi_get_ctldata(spi);
  379. struct driver_data *drv_data =
  380. spi_controller_get_devdata(spi->controller);
  381. if (drv_data->ssp_type == CE4100_SSP) {
  382. pxa2xx_spi_write(drv_data, SSSR, chip->frm);
  383. return;
  384. }
  385. if (chip->cs_control) {
  386. chip->cs_control(PXA2XX_CS_ASSERT);
  387. return;
  388. }
  389. if (chip->gpiod_cs) {
  390. gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
  391. return;
  392. }
  393. if (is_lpss_ssp(drv_data))
  394. lpss_ssp_cs_control(spi, true);
  395. }
  396. static void cs_deassert(struct spi_device *spi)
  397. {
  398. struct chip_data *chip = spi_get_ctldata(spi);
  399. struct driver_data *drv_data =
  400. spi_controller_get_devdata(spi->controller);
  401. unsigned long timeout;
  402. if (drv_data->ssp_type == CE4100_SSP)
  403. return;
  404. /* Wait until SSP becomes idle before deasserting the CS */
  405. timeout = jiffies + msecs_to_jiffies(10);
  406. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
  407. !time_after(jiffies, timeout))
  408. cpu_relax();
  409. if (chip->cs_control) {
  410. chip->cs_control(PXA2XX_CS_DEASSERT);
  411. return;
  412. }
  413. if (chip->gpiod_cs) {
  414. gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
  415. return;
  416. }
  417. if (is_lpss_ssp(drv_data))
  418. lpss_ssp_cs_control(spi, false);
  419. }
  420. static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
  421. {
  422. if (level)
  423. cs_deassert(spi);
  424. else
  425. cs_assert(spi);
  426. }
  427. int pxa2xx_spi_flush(struct driver_data *drv_data)
  428. {
  429. unsigned long limit = loops_per_jiffy << 1;
  430. do {
  431. while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  432. pxa2xx_spi_read(drv_data, SSDR);
  433. } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
  434. write_SSSR_CS(drv_data, SSSR_ROR);
  435. return limit;
  436. }
  437. static int null_writer(struct driver_data *drv_data)
  438. {
  439. u8 n_bytes = drv_data->n_bytes;
  440. if (pxa2xx_spi_txfifo_full(drv_data)
  441. || (drv_data->tx == drv_data->tx_end))
  442. return 0;
  443. pxa2xx_spi_write(drv_data, SSDR, 0);
  444. drv_data->tx += n_bytes;
  445. return 1;
  446. }
  447. static int null_reader(struct driver_data *drv_data)
  448. {
  449. u8 n_bytes = drv_data->n_bytes;
  450. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  451. && (drv_data->rx < drv_data->rx_end)) {
  452. pxa2xx_spi_read(drv_data, SSDR);
  453. drv_data->rx += n_bytes;
  454. }
  455. return drv_data->rx == drv_data->rx_end;
  456. }
  457. static int u8_writer(struct driver_data *drv_data)
  458. {
  459. if (pxa2xx_spi_txfifo_full(drv_data)
  460. || (drv_data->tx == drv_data->tx_end))
  461. return 0;
  462. pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
  463. ++drv_data->tx;
  464. return 1;
  465. }
  466. static int u8_reader(struct driver_data *drv_data)
  467. {
  468. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  469. && (drv_data->rx < drv_data->rx_end)) {
  470. *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  471. ++drv_data->rx;
  472. }
  473. return drv_data->rx == drv_data->rx_end;
  474. }
  475. static int u16_writer(struct driver_data *drv_data)
  476. {
  477. if (pxa2xx_spi_txfifo_full(drv_data)
  478. || (drv_data->tx == drv_data->tx_end))
  479. return 0;
  480. pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
  481. drv_data->tx += 2;
  482. return 1;
  483. }
  484. static int u16_reader(struct driver_data *drv_data)
  485. {
  486. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  487. && (drv_data->rx < drv_data->rx_end)) {
  488. *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  489. drv_data->rx += 2;
  490. }
  491. return drv_data->rx == drv_data->rx_end;
  492. }
  493. static int u32_writer(struct driver_data *drv_data)
  494. {
  495. if (pxa2xx_spi_txfifo_full(drv_data)
  496. || (drv_data->tx == drv_data->tx_end))
  497. return 0;
  498. pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
  499. drv_data->tx += 4;
  500. return 1;
  501. }
  502. static int u32_reader(struct driver_data *drv_data)
  503. {
  504. while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
  505. && (drv_data->rx < drv_data->rx_end)) {
  506. *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
  507. drv_data->rx += 4;
  508. }
  509. return drv_data->rx == drv_data->rx_end;
  510. }
  511. static void reset_sccr1(struct driver_data *drv_data)
  512. {
  513. struct chip_data *chip =
  514. spi_get_ctldata(drv_data->master->cur_msg->spi);
  515. u32 sccr1_reg;
  516. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
  517. switch (drv_data->ssp_type) {
  518. case QUARK_X1000_SSP:
  519. sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
  520. break;
  521. case CE4100_SSP:
  522. sccr1_reg &= ~CE4100_SSCR1_RFT;
  523. break;
  524. default:
  525. sccr1_reg &= ~SSCR1_RFT;
  526. break;
  527. }
  528. sccr1_reg |= chip->threshold;
  529. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  530. }
  531. static void int_error_stop(struct driver_data *drv_data, const char* msg)
  532. {
  533. /* Stop and reset SSP */
  534. write_SSSR_CS(drv_data, drv_data->clear_sr);
  535. reset_sccr1(drv_data);
  536. if (!pxa25x_ssp_comp(drv_data))
  537. pxa2xx_spi_write(drv_data, SSTO, 0);
  538. pxa2xx_spi_flush(drv_data);
  539. pxa2xx_spi_write(drv_data, SSCR0,
  540. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  541. dev_err(&drv_data->pdev->dev, "%s\n", msg);
  542. drv_data->master->cur_msg->status = -EIO;
  543. spi_finalize_current_transfer(drv_data->master);
  544. }
  545. static void int_transfer_complete(struct driver_data *drv_data)
  546. {
  547. /* Clear and disable interrupts */
  548. write_SSSR_CS(drv_data, drv_data->clear_sr);
  549. reset_sccr1(drv_data);
  550. if (!pxa25x_ssp_comp(drv_data))
  551. pxa2xx_spi_write(drv_data, SSTO, 0);
  552. spi_finalize_current_transfer(drv_data->master);
  553. }
  554. static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
  555. {
  556. u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
  557. drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
  558. u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
  559. if (irq_status & SSSR_ROR) {
  560. int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
  561. return IRQ_HANDLED;
  562. }
  563. if (irq_status & SSSR_TINT) {
  564. pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
  565. if (drv_data->read(drv_data)) {
  566. int_transfer_complete(drv_data);
  567. return IRQ_HANDLED;
  568. }
  569. }
  570. /* Drain rx fifo, Fill tx fifo and prevent overruns */
  571. do {
  572. if (drv_data->read(drv_data)) {
  573. int_transfer_complete(drv_data);
  574. return IRQ_HANDLED;
  575. }
  576. } while (drv_data->write(drv_data));
  577. if (drv_data->read(drv_data)) {
  578. int_transfer_complete(drv_data);
  579. return IRQ_HANDLED;
  580. }
  581. if (drv_data->tx == drv_data->tx_end) {
  582. u32 bytes_left;
  583. u32 sccr1_reg;
  584. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  585. sccr1_reg &= ~SSCR1_TIE;
  586. /*
  587. * PXA25x_SSP has no timeout, set up rx threshould for the
  588. * remaining RX bytes.
  589. */
  590. if (pxa25x_ssp_comp(drv_data)) {
  591. u32 rx_thre;
  592. pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
  593. bytes_left = drv_data->rx_end - drv_data->rx;
  594. switch (drv_data->n_bytes) {
  595. case 4:
  596. bytes_left >>= 1;
  597. case 2:
  598. bytes_left >>= 1;
  599. }
  600. rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
  601. if (rx_thre > bytes_left)
  602. rx_thre = bytes_left;
  603. pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
  604. }
  605. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  606. }
  607. /* We did something */
  608. return IRQ_HANDLED;
  609. }
  610. static void handle_bad_msg(struct driver_data *drv_data)
  611. {
  612. pxa2xx_spi_write(drv_data, SSCR0,
  613. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  614. pxa2xx_spi_write(drv_data, SSCR1,
  615. pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
  616. if (!pxa25x_ssp_comp(drv_data))
  617. pxa2xx_spi_write(drv_data, SSTO, 0);
  618. write_SSSR_CS(drv_data, drv_data->clear_sr);
  619. dev_err(&drv_data->pdev->dev,
  620. "bad message state in interrupt handler\n");
  621. }
  622. static irqreturn_t ssp_int(int irq, void *dev_id)
  623. {
  624. struct driver_data *drv_data = dev_id;
  625. u32 sccr1_reg;
  626. u32 mask = drv_data->mask_sr;
  627. u32 status;
  628. /*
  629. * The IRQ might be shared with other peripherals so we must first
  630. * check that are we RPM suspended or not. If we are we assume that
  631. * the IRQ was not for us (we shouldn't be RPM suspended when the
  632. * interrupt is enabled).
  633. */
  634. if (pm_runtime_suspended(&drv_data->pdev->dev))
  635. return IRQ_NONE;
  636. /*
  637. * If the device is not yet in RPM suspended state and we get an
  638. * interrupt that is meant for another device, check if status bits
  639. * are all set to one. That means that the device is already
  640. * powered off.
  641. */
  642. status = pxa2xx_spi_read(drv_data, SSSR);
  643. if (status == ~0)
  644. return IRQ_NONE;
  645. sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
  646. /* Ignore possible writes if we don't need to write */
  647. if (!(sccr1_reg & SSCR1_TIE))
  648. mask &= ~SSSR_TFS;
  649. /* Ignore RX timeout interrupt if it is disabled */
  650. if (!(sccr1_reg & SSCR1_TINTE))
  651. mask &= ~SSSR_TINT;
  652. if (!(status & mask))
  653. return IRQ_NONE;
  654. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
  655. pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
  656. if (!drv_data->master->cur_msg) {
  657. handle_bad_msg(drv_data);
  658. /* Never fail */
  659. return IRQ_HANDLED;
  660. }
  661. return drv_data->transfer_handler(drv_data);
  662. }
  663. /*
  664. * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
  665. * input frequency by fractions of 2^24. It also has a divider by 5.
  666. *
  667. * There are formulas to get baud rate value for given input frequency and
  668. * divider parameters, such as DDS_CLK_RATE and SCR:
  669. *
  670. * Fsys = 200MHz
  671. *
  672. * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
  673. * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
  674. *
  675. * DDS_CLK_RATE either 2^n or 2^n / 5.
  676. * SCR is in range 0 .. 255
  677. *
  678. * Divisor = 5^i * 2^j * 2 * k
  679. * i = [0, 1] i = 1 iff j = 0 or j > 3
  680. * j = [0, 23] j = 0 iff i = 1
  681. * k = [1, 256]
  682. * Special case: j = 0, i = 1: Divisor = 2 / 5
  683. *
  684. * Accordingly to the specification the recommended values for DDS_CLK_RATE
  685. * are:
  686. * Case 1: 2^n, n = [0, 23]
  687. * Case 2: 2^24 * 2 / 5 (0x666666)
  688. * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
  689. *
  690. * In all cases the lowest possible value is better.
  691. *
  692. * The function calculates parameters for all cases and chooses the one closest
  693. * to the asked baud rate.
  694. */
  695. static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
  696. {
  697. unsigned long xtal = 200000000;
  698. unsigned long fref = xtal / 2; /* mandatory division by 2,
  699. see (2) */
  700. /* case 3 */
  701. unsigned long fref1 = fref / 2; /* case 1 */
  702. unsigned long fref2 = fref * 2 / 5; /* case 2 */
  703. unsigned long scale;
  704. unsigned long q, q1, q2;
  705. long r, r1, r2;
  706. u32 mul;
  707. /* Case 1 */
  708. /* Set initial value for DDS_CLK_RATE */
  709. mul = (1 << 24) >> 1;
  710. /* Calculate initial quot */
  711. q1 = DIV_ROUND_UP(fref1, rate);
  712. /* Scale q1 if it's too big */
  713. if (q1 > 256) {
  714. /* Scale q1 to range [1, 512] */
  715. scale = fls_long(q1 - 1);
  716. if (scale > 9) {
  717. q1 >>= scale - 9;
  718. mul >>= scale - 9;
  719. }
  720. /* Round the result if we have a remainder */
  721. q1 += q1 & 1;
  722. }
  723. /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
  724. scale = __ffs(q1);
  725. q1 >>= scale;
  726. mul >>= scale;
  727. /* Get the remainder */
  728. r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
  729. /* Case 2 */
  730. q2 = DIV_ROUND_UP(fref2, rate);
  731. r2 = abs(fref2 / q2 - rate);
  732. /*
  733. * Choose the best between two: less remainder we have the better. We
  734. * can't go case 2 if q2 is greater than 256 since SCR register can
  735. * hold only values 0 .. 255.
  736. */
  737. if (r2 >= r1 || q2 > 256) {
  738. /* case 1 is better */
  739. r = r1;
  740. q = q1;
  741. } else {
  742. /* case 2 is better */
  743. r = r2;
  744. q = q2;
  745. mul = (1 << 24) * 2 / 5;
  746. }
  747. /* Check case 3 only if the divisor is big enough */
  748. if (fref / rate >= 80) {
  749. u64 fssp;
  750. u32 m;
  751. /* Calculate initial quot */
  752. q1 = DIV_ROUND_UP(fref, rate);
  753. m = (1 << 24) / q1;
  754. /* Get the remainder */
  755. fssp = (u64)fref * m;
  756. do_div(fssp, 1 << 24);
  757. r1 = abs(fssp - rate);
  758. /* Choose this one if it suits better */
  759. if (r1 < r) {
  760. /* case 3 is better */
  761. q = 1;
  762. mul = m;
  763. }
  764. }
  765. *dds = mul;
  766. return q - 1;
  767. }
  768. static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
  769. {
  770. unsigned long ssp_clk = drv_data->master->max_speed_hz;
  771. const struct ssp_device *ssp = drv_data->ssp;
  772. rate = min_t(int, ssp_clk, rate);
  773. /*
  774. * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
  775. * that the SSP transmission rate can be greater than the device rate
  776. */
  777. if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
  778. return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
  779. else
  780. return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
  781. }
  782. static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
  783. int rate)
  784. {
  785. struct chip_data *chip =
  786. spi_get_ctldata(drv_data->master->cur_msg->spi);
  787. unsigned int clk_div;
  788. switch (drv_data->ssp_type) {
  789. case QUARK_X1000_SSP:
  790. clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
  791. break;
  792. default:
  793. clk_div = ssp_get_clk_div(drv_data, rate);
  794. break;
  795. }
  796. return clk_div << 8;
  797. }
  798. static bool pxa2xx_spi_can_dma(struct spi_controller *master,
  799. struct spi_device *spi,
  800. struct spi_transfer *xfer)
  801. {
  802. struct chip_data *chip = spi_get_ctldata(spi);
  803. return chip->enable_dma &&
  804. xfer->len <= MAX_DMA_LEN &&
  805. xfer->len >= chip->dma_burst_size;
  806. }
  807. static int pxa2xx_spi_transfer_one(struct spi_controller *master,
  808. struct spi_device *spi,
  809. struct spi_transfer *transfer)
  810. {
  811. struct driver_data *drv_data = spi_controller_get_devdata(master);
  812. struct spi_message *message = master->cur_msg;
  813. struct chip_data *chip = spi_get_ctldata(message->spi);
  814. u32 dma_thresh = chip->dma_threshold;
  815. u32 dma_burst = chip->dma_burst_size;
  816. u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
  817. u32 clk_div;
  818. u8 bits;
  819. u32 speed;
  820. u32 cr0;
  821. u32 cr1;
  822. int err;
  823. int dma_mapped;
  824. /* Check if we can DMA this transfer */
  825. if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
  826. /* reject already-mapped transfers; PIO won't always work */
  827. if (message->is_dma_mapped
  828. || transfer->rx_dma || transfer->tx_dma) {
  829. dev_err(&drv_data->pdev->dev,
  830. "Mapped transfer length of %u is greater than %d\n",
  831. transfer->len, MAX_DMA_LEN);
  832. return -EINVAL;
  833. }
  834. /* warn ... we force this to PIO mode */
  835. dev_warn_ratelimited(&message->spi->dev,
  836. "DMA disabled for transfer length %ld greater than %d\n",
  837. (long)transfer->len, MAX_DMA_LEN);
  838. }
  839. /* Setup the transfer state based on the type of transfer */
  840. if (pxa2xx_spi_flush(drv_data) == 0) {
  841. dev_err(&drv_data->pdev->dev, "Flush failed\n");
  842. return -EIO;
  843. }
  844. drv_data->n_bytes = chip->n_bytes;
  845. drv_data->tx = (void *)transfer->tx_buf;
  846. drv_data->tx_end = drv_data->tx + transfer->len;
  847. drv_data->rx = transfer->rx_buf;
  848. drv_data->rx_end = drv_data->rx + transfer->len;
  849. drv_data->write = drv_data->tx ? chip->write : null_writer;
  850. drv_data->read = drv_data->rx ? chip->read : null_reader;
  851. /* Change speed and bit per word on a per transfer */
  852. bits = transfer->bits_per_word;
  853. speed = transfer->speed_hz;
  854. clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
  855. if (bits <= 8) {
  856. drv_data->n_bytes = 1;
  857. drv_data->read = drv_data->read != null_reader ?
  858. u8_reader : null_reader;
  859. drv_data->write = drv_data->write != null_writer ?
  860. u8_writer : null_writer;
  861. } else if (bits <= 16) {
  862. drv_data->n_bytes = 2;
  863. drv_data->read = drv_data->read != null_reader ?
  864. u16_reader : null_reader;
  865. drv_data->write = drv_data->write != null_writer ?
  866. u16_writer : null_writer;
  867. } else if (bits <= 32) {
  868. drv_data->n_bytes = 4;
  869. drv_data->read = drv_data->read != null_reader ?
  870. u32_reader : null_reader;
  871. drv_data->write = drv_data->write != null_writer ?
  872. u32_writer : null_writer;
  873. }
  874. /*
  875. * if bits/word is changed in dma mode, then must check the
  876. * thresholds and burst also
  877. */
  878. if (chip->enable_dma) {
  879. if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
  880. message->spi,
  881. bits, &dma_burst,
  882. &dma_thresh))
  883. dev_warn_ratelimited(&message->spi->dev,
  884. "DMA burst size reduced to match bits_per_word\n");
  885. }
  886. dma_mapped = master->can_dma &&
  887. master->can_dma(master, message->spi, transfer) &&
  888. master->cur_msg_mapped;
  889. if (dma_mapped) {
  890. /* Ensure we have the correct interrupt handler */
  891. drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
  892. err = pxa2xx_spi_dma_prepare(drv_data, transfer);
  893. if (err)
  894. return err;
  895. /* Clear status and start DMA engine */
  896. cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
  897. pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
  898. pxa2xx_spi_dma_start(drv_data);
  899. } else {
  900. /* Ensure we have the correct interrupt handler */
  901. drv_data->transfer_handler = interrupt_transfer;
  902. /* Clear status */
  903. cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
  904. write_SSSR_CS(drv_data, drv_data->clear_sr);
  905. }
  906. /* NOTE: PXA25x_SSP _could_ use external clocking ... */
  907. cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
  908. if (!pxa25x_ssp_comp(drv_data))
  909. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  910. master->max_speed_hz
  911. / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
  912. dma_mapped ? "DMA" : "PIO");
  913. else
  914. dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
  915. master->max_speed_hz / 2
  916. / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
  917. dma_mapped ? "DMA" : "PIO");
  918. if (is_lpss_ssp(drv_data)) {
  919. if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
  920. != chip->lpss_rx_threshold)
  921. pxa2xx_spi_write(drv_data, SSIRF,
  922. chip->lpss_rx_threshold);
  923. if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
  924. != chip->lpss_tx_threshold)
  925. pxa2xx_spi_write(drv_data, SSITF,
  926. chip->lpss_tx_threshold);
  927. }
  928. if (is_quark_x1000_ssp(drv_data) &&
  929. (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
  930. pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
  931. /* see if we need to reload the config registers */
  932. if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
  933. || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
  934. != (cr1 & change_mask)) {
  935. /* stop the SSP, and update the other bits */
  936. pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
  937. if (!pxa25x_ssp_comp(drv_data))
  938. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  939. /* first set CR1 without interrupt and service enables */
  940. pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
  941. /* restart the SSP */
  942. pxa2xx_spi_write(drv_data, SSCR0, cr0);
  943. } else {
  944. if (!pxa25x_ssp_comp(drv_data))
  945. pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
  946. }
  947. /*
  948. * Release the data by enabling service requests and interrupts,
  949. * without changing any mode bits
  950. */
  951. pxa2xx_spi_write(drv_data, SSCR1, cr1);
  952. return 1;
  953. }
  954. static void pxa2xx_spi_handle_err(struct spi_controller *master,
  955. struct spi_message *msg)
  956. {
  957. struct driver_data *drv_data = spi_controller_get_devdata(master);
  958. /* Disable the SSP */
  959. pxa2xx_spi_write(drv_data, SSCR0,
  960. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  961. /* Clear and disable interrupts and service requests */
  962. write_SSSR_CS(drv_data, drv_data->clear_sr);
  963. pxa2xx_spi_write(drv_data, SSCR1,
  964. pxa2xx_spi_read(drv_data, SSCR1)
  965. & ~(drv_data->int_cr1 | drv_data->dma_cr1));
  966. if (!pxa25x_ssp_comp(drv_data))
  967. pxa2xx_spi_write(drv_data, SSTO, 0);
  968. /*
  969. * Stop the DMA if running. Note DMA callback handler may have unset
  970. * the dma_running already, which is fine as stopping is not needed
  971. * then but we shouldn't rely this flag for anything else than
  972. * stopping. For instance to differentiate between PIO and DMA
  973. * transfers.
  974. */
  975. if (atomic_read(&drv_data->dma_running))
  976. pxa2xx_spi_dma_stop(drv_data);
  977. }
  978. static int pxa2xx_spi_unprepare_transfer(struct spi_controller *master)
  979. {
  980. struct driver_data *drv_data = spi_controller_get_devdata(master);
  981. /* Disable the SSP now */
  982. pxa2xx_spi_write(drv_data, SSCR0,
  983. pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
  984. return 0;
  985. }
  986. static int setup_cs(struct spi_device *spi, struct chip_data *chip,
  987. struct pxa2xx_spi_chip *chip_info)
  988. {
  989. struct driver_data *drv_data =
  990. spi_controller_get_devdata(spi->controller);
  991. struct gpio_desc *gpiod;
  992. int err = 0;
  993. if (chip == NULL)
  994. return 0;
  995. if (drv_data->cs_gpiods) {
  996. gpiod = drv_data->cs_gpiods[spi->chip_select];
  997. if (gpiod) {
  998. chip->gpiod_cs = gpiod;
  999. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1000. gpiod_set_value(gpiod, chip->gpio_cs_inverted);
  1001. }
  1002. return 0;
  1003. }
  1004. if (chip_info == NULL)
  1005. return 0;
  1006. /* NOTE: setup() can be called multiple times, possibly with
  1007. * different chip_info, release previously requested GPIO
  1008. */
  1009. if (chip->gpiod_cs) {
  1010. gpiod_put(chip->gpiod_cs);
  1011. chip->gpiod_cs = NULL;
  1012. }
  1013. /* If (*cs_control) is provided, ignore GPIO chip select */
  1014. if (chip_info->cs_control) {
  1015. chip->cs_control = chip_info->cs_control;
  1016. return 0;
  1017. }
  1018. if (gpio_is_valid(chip_info->gpio_cs)) {
  1019. err = gpio_request(chip_info->gpio_cs, "SPI_CS");
  1020. if (err) {
  1021. dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
  1022. chip_info->gpio_cs);
  1023. return err;
  1024. }
  1025. gpiod = gpio_to_desc(chip_info->gpio_cs);
  1026. chip->gpiod_cs = gpiod;
  1027. chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
  1028. err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
  1029. }
  1030. return err;
  1031. }
  1032. static int setup(struct spi_device *spi)
  1033. {
  1034. struct pxa2xx_spi_chip *chip_info;
  1035. struct chip_data *chip;
  1036. const struct lpss_config *config;
  1037. struct driver_data *drv_data =
  1038. spi_controller_get_devdata(spi->controller);
  1039. uint tx_thres, tx_hi_thres, rx_thres;
  1040. switch (drv_data->ssp_type) {
  1041. case QUARK_X1000_SSP:
  1042. tx_thres = TX_THRESH_QUARK_X1000_DFLT;
  1043. tx_hi_thres = 0;
  1044. rx_thres = RX_THRESH_QUARK_X1000_DFLT;
  1045. break;
  1046. case CE4100_SSP:
  1047. tx_thres = TX_THRESH_CE4100_DFLT;
  1048. tx_hi_thres = 0;
  1049. rx_thres = RX_THRESH_CE4100_DFLT;
  1050. break;
  1051. case LPSS_LPT_SSP:
  1052. case LPSS_BYT_SSP:
  1053. case LPSS_BSW_SSP:
  1054. case LPSS_SPT_SSP:
  1055. case LPSS_BXT_SSP:
  1056. case LPSS_CNL_SSP:
  1057. config = lpss_get_config(drv_data);
  1058. tx_thres = config->tx_threshold_lo;
  1059. tx_hi_thres = config->tx_threshold_hi;
  1060. rx_thres = config->rx_threshold;
  1061. break;
  1062. default:
  1063. tx_thres = TX_THRESH_DFLT;
  1064. tx_hi_thres = 0;
  1065. rx_thres = RX_THRESH_DFLT;
  1066. break;
  1067. }
  1068. /* Only alloc on first setup */
  1069. chip = spi_get_ctldata(spi);
  1070. if (!chip) {
  1071. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  1072. if (!chip)
  1073. return -ENOMEM;
  1074. if (drv_data->ssp_type == CE4100_SSP) {
  1075. if (spi->chip_select > 4) {
  1076. dev_err(&spi->dev,
  1077. "failed setup: cs number must not be > 4.\n");
  1078. kfree(chip);
  1079. return -EINVAL;
  1080. }
  1081. chip->frm = spi->chip_select;
  1082. }
  1083. chip->enable_dma = drv_data->master_info->enable_dma;
  1084. chip->timeout = TIMOUT_DFLT;
  1085. }
  1086. /* protocol drivers may change the chip settings, so...
  1087. * if chip_info exists, use it */
  1088. chip_info = spi->controller_data;
  1089. /* chip_info isn't always needed */
  1090. chip->cr1 = 0;
  1091. if (chip_info) {
  1092. if (chip_info->timeout)
  1093. chip->timeout = chip_info->timeout;
  1094. if (chip_info->tx_threshold)
  1095. tx_thres = chip_info->tx_threshold;
  1096. if (chip_info->tx_hi_threshold)
  1097. tx_hi_thres = chip_info->tx_hi_threshold;
  1098. if (chip_info->rx_threshold)
  1099. rx_thres = chip_info->rx_threshold;
  1100. chip->dma_threshold = 0;
  1101. if (chip_info->enable_loopback)
  1102. chip->cr1 = SSCR1_LBM;
  1103. }
  1104. chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
  1105. chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
  1106. | SSITF_TxHiThresh(tx_hi_thres);
  1107. /* set dma burst and threshold outside of chip_info path so that if
  1108. * chip_info goes away after setting chip->enable_dma, the
  1109. * burst and threshold can still respond to changes in bits_per_word */
  1110. if (chip->enable_dma) {
  1111. /* set up legal burst and threshold for dma */
  1112. if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
  1113. spi->bits_per_word,
  1114. &chip->dma_burst_size,
  1115. &chip->dma_threshold)) {
  1116. dev_warn(&spi->dev,
  1117. "in setup: DMA burst size reduced to match bits_per_word\n");
  1118. }
  1119. }
  1120. switch (drv_data->ssp_type) {
  1121. case QUARK_X1000_SSP:
  1122. chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
  1123. & QUARK_X1000_SSCR1_RFT)
  1124. | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
  1125. & QUARK_X1000_SSCR1_TFT);
  1126. break;
  1127. case CE4100_SSP:
  1128. chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
  1129. (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
  1130. break;
  1131. default:
  1132. chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
  1133. (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
  1134. break;
  1135. }
  1136. chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  1137. chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  1138. | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  1139. if (spi->mode & SPI_LOOP)
  1140. chip->cr1 |= SSCR1_LBM;
  1141. if (spi->bits_per_word <= 8) {
  1142. chip->n_bytes = 1;
  1143. chip->read = u8_reader;
  1144. chip->write = u8_writer;
  1145. } else if (spi->bits_per_word <= 16) {
  1146. chip->n_bytes = 2;
  1147. chip->read = u16_reader;
  1148. chip->write = u16_writer;
  1149. } else if (spi->bits_per_word <= 32) {
  1150. chip->n_bytes = 4;
  1151. chip->read = u32_reader;
  1152. chip->write = u32_writer;
  1153. }
  1154. spi_set_ctldata(spi, chip);
  1155. if (drv_data->ssp_type == CE4100_SSP)
  1156. return 0;
  1157. return setup_cs(spi, chip, chip_info);
  1158. }
  1159. static void cleanup(struct spi_device *spi)
  1160. {
  1161. struct chip_data *chip = spi_get_ctldata(spi);
  1162. struct driver_data *drv_data =
  1163. spi_controller_get_devdata(spi->controller);
  1164. if (!chip)
  1165. return;
  1166. if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
  1167. chip->gpiod_cs)
  1168. gpiod_put(chip->gpiod_cs);
  1169. kfree(chip);
  1170. }
  1171. #ifdef CONFIG_PCI
  1172. #ifdef CONFIG_ACPI
  1173. static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
  1174. { "INT33C0", LPSS_LPT_SSP },
  1175. { "INT33C1", LPSS_LPT_SSP },
  1176. { "INT3430", LPSS_LPT_SSP },
  1177. { "INT3431", LPSS_LPT_SSP },
  1178. { "80860F0E", LPSS_BYT_SSP },
  1179. { "8086228E", LPSS_BSW_SSP },
  1180. { },
  1181. };
  1182. MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
  1183. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1184. {
  1185. unsigned int devid;
  1186. int port_id = -1;
  1187. if (adev && adev->pnp.unique_id &&
  1188. !kstrtouint(adev->pnp.unique_id, 0, &devid))
  1189. port_id = devid;
  1190. return port_id;
  1191. }
  1192. #else /* !CONFIG_ACPI */
  1193. static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
  1194. {
  1195. return -1;
  1196. }
  1197. #endif
  1198. /*
  1199. * PCI IDs of compound devices that integrate both host controller and private
  1200. * integrated DMA engine. Please note these are not used in module
  1201. * autoloading and probing in this module but matching the LPSS SSP type.
  1202. */
  1203. static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
  1204. /* SPT-LP */
  1205. { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
  1206. { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
  1207. /* SPT-H */
  1208. { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
  1209. { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
  1210. /* KBL-H */
  1211. { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
  1212. { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
  1213. /* BXT A-Step */
  1214. { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
  1215. { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
  1216. { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
  1217. /* BXT B-Step */
  1218. { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
  1219. { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
  1220. { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
  1221. /* GLK */
  1222. { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
  1223. { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
  1224. { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
  1225. /* ICL-LP */
  1226. { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
  1227. { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
  1228. { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
  1229. /* APL */
  1230. { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
  1231. { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
  1232. { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
  1233. /* CNL-LP */
  1234. { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
  1235. { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
  1236. { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
  1237. /* CNL-H */
  1238. { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
  1239. { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
  1240. { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
  1241. { },
  1242. };
  1243. static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
  1244. {
  1245. return param == chan->device->dev;
  1246. }
  1247. static struct pxa2xx_spi_master *
  1248. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1249. {
  1250. struct pxa2xx_spi_master *pdata;
  1251. struct acpi_device *adev;
  1252. struct ssp_device *ssp;
  1253. struct resource *res;
  1254. const struct acpi_device_id *adev_id = NULL;
  1255. const struct pci_device_id *pcidev_id = NULL;
  1256. int type;
  1257. adev = ACPI_COMPANION(&pdev->dev);
  1258. if (dev_is_pci(pdev->dev.parent))
  1259. pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
  1260. to_pci_dev(pdev->dev.parent));
  1261. else if (adev)
  1262. adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
  1263. &pdev->dev);
  1264. else
  1265. return NULL;
  1266. if (adev_id)
  1267. type = (int)adev_id->driver_data;
  1268. else if (pcidev_id)
  1269. type = (int)pcidev_id->driver_data;
  1270. else
  1271. return NULL;
  1272. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  1273. if (!pdata)
  1274. return NULL;
  1275. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1276. if (!res)
  1277. return NULL;
  1278. ssp = &pdata->ssp;
  1279. ssp->phys_base = res->start;
  1280. ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
  1281. if (IS_ERR(ssp->mmio_base))
  1282. return NULL;
  1283. if (pcidev_id) {
  1284. pdata->tx_param = pdev->dev.parent;
  1285. pdata->rx_param = pdev->dev.parent;
  1286. pdata->dma_filter = pxa2xx_spi_idma_filter;
  1287. }
  1288. ssp->clk = devm_clk_get(&pdev->dev, NULL);
  1289. if (IS_ERR(ssp->clk))
  1290. return NULL;
  1291. ssp->irq = platform_get_irq(pdev, 0);
  1292. if (ssp->irq < 0)
  1293. return NULL;
  1294. ssp->type = type;
  1295. ssp->pdev = pdev;
  1296. ssp->port_id = pxa2xx_spi_get_port_id(adev);
  1297. pdata->num_chipselect = 1;
  1298. pdata->enable_dma = true;
  1299. return pdata;
  1300. }
  1301. #else /* !CONFIG_PCI */
  1302. static inline struct pxa2xx_spi_master *
  1303. pxa2xx_spi_init_pdata(struct platform_device *pdev)
  1304. {
  1305. return NULL;
  1306. }
  1307. #endif
  1308. static int pxa2xx_spi_fw_translate_cs(struct spi_controller *master,
  1309. unsigned int cs)
  1310. {
  1311. struct driver_data *drv_data = spi_controller_get_devdata(master);
  1312. if (has_acpi_companion(&drv_data->pdev->dev)) {
  1313. switch (drv_data->ssp_type) {
  1314. /*
  1315. * For Atoms the ACPI DeviceSelection used by the Windows
  1316. * driver starts from 1 instead of 0 so translate it here
  1317. * to match what Linux expects.
  1318. */
  1319. case LPSS_BYT_SSP:
  1320. case LPSS_BSW_SSP:
  1321. return cs - 1;
  1322. default:
  1323. break;
  1324. }
  1325. }
  1326. return cs;
  1327. }
  1328. static int pxa2xx_spi_probe(struct platform_device *pdev)
  1329. {
  1330. struct device *dev = &pdev->dev;
  1331. struct pxa2xx_spi_master *platform_info;
  1332. struct spi_controller *master;
  1333. struct driver_data *drv_data;
  1334. struct ssp_device *ssp;
  1335. const struct lpss_config *config;
  1336. int status, count;
  1337. u32 tmp;
  1338. platform_info = dev_get_platdata(dev);
  1339. if (!platform_info) {
  1340. platform_info = pxa2xx_spi_init_pdata(pdev);
  1341. if (!platform_info) {
  1342. dev_err(&pdev->dev, "missing platform data\n");
  1343. return -ENODEV;
  1344. }
  1345. }
  1346. ssp = pxa_ssp_request(pdev->id, pdev->name);
  1347. if (!ssp)
  1348. ssp = &platform_info->ssp;
  1349. if (!ssp->mmio_base) {
  1350. dev_err(&pdev->dev, "failed to get ssp\n");
  1351. return -ENODEV;
  1352. }
  1353. master = spi_alloc_master(dev, sizeof(struct driver_data));
  1354. if (!master) {
  1355. dev_err(&pdev->dev, "cannot alloc spi_master\n");
  1356. pxa_ssp_free(ssp);
  1357. return -ENOMEM;
  1358. }
  1359. drv_data = spi_controller_get_devdata(master);
  1360. drv_data->master = master;
  1361. drv_data->master_info = platform_info;
  1362. drv_data->pdev = pdev;
  1363. drv_data->ssp = ssp;
  1364. master->dev.of_node = pdev->dev.of_node;
  1365. /* the spi->mode bits understood by this driver: */
  1366. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
  1367. master->bus_num = ssp->port_id;
  1368. master->dma_alignment = DMA_ALIGNMENT;
  1369. master->cleanup = cleanup;
  1370. master->setup = setup;
  1371. master->set_cs = pxa2xx_spi_set_cs;
  1372. master->transfer_one = pxa2xx_spi_transfer_one;
  1373. master->handle_err = pxa2xx_spi_handle_err;
  1374. master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
  1375. master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
  1376. master->auto_runtime_pm = true;
  1377. master->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
  1378. drv_data->ssp_type = ssp->type;
  1379. drv_data->ioaddr = ssp->mmio_base;
  1380. drv_data->ssdr_physical = ssp->phys_base + SSDR;
  1381. if (pxa25x_ssp_comp(drv_data)) {
  1382. switch (drv_data->ssp_type) {
  1383. case QUARK_X1000_SSP:
  1384. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1385. break;
  1386. default:
  1387. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1388. break;
  1389. }
  1390. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
  1391. drv_data->dma_cr1 = 0;
  1392. drv_data->clear_sr = SSSR_ROR;
  1393. drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1394. } else {
  1395. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1396. drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  1397. drv_data->dma_cr1 = DEFAULT_DMA_CR1;
  1398. drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
  1399. drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
  1400. }
  1401. status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
  1402. drv_data);
  1403. if (status < 0) {
  1404. dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
  1405. goto out_error_master_alloc;
  1406. }
  1407. /* Setup DMA if requested */
  1408. if (platform_info->enable_dma) {
  1409. status = pxa2xx_spi_dma_setup(drv_data);
  1410. if (status) {
  1411. dev_dbg(dev, "no DMA channels available, using PIO\n");
  1412. platform_info->enable_dma = false;
  1413. } else {
  1414. master->can_dma = pxa2xx_spi_can_dma;
  1415. master->max_dma_len = MAX_DMA_LEN;
  1416. }
  1417. }
  1418. /* Enable SOC clock */
  1419. status = clk_prepare_enable(ssp->clk);
  1420. if (status)
  1421. goto out_error_dma_irq_alloc;
  1422. master->max_speed_hz = clk_get_rate(ssp->clk);
  1423. /* Load default SSP configuration */
  1424. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1425. switch (drv_data->ssp_type) {
  1426. case QUARK_X1000_SSP:
  1427. tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
  1428. QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
  1429. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1430. /* using the Motorola SPI protocol and use 8 bit frame */
  1431. tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
  1432. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1433. break;
  1434. case CE4100_SSP:
  1435. tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
  1436. CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
  1437. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1438. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1439. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1440. break;
  1441. default:
  1442. tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
  1443. SSCR1_TxTresh(TX_THRESH_DFLT);
  1444. pxa2xx_spi_write(drv_data, SSCR1, tmp);
  1445. tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
  1446. pxa2xx_spi_write(drv_data, SSCR0, tmp);
  1447. break;
  1448. }
  1449. if (!pxa25x_ssp_comp(drv_data))
  1450. pxa2xx_spi_write(drv_data, SSTO, 0);
  1451. if (!is_quark_x1000_ssp(drv_data))
  1452. pxa2xx_spi_write(drv_data, SSPSP, 0);
  1453. if (is_lpss_ssp(drv_data)) {
  1454. lpss_ssp_setup(drv_data);
  1455. config = lpss_get_config(drv_data);
  1456. if (config->reg_capabilities >= 0) {
  1457. tmp = __lpss_ssp_read_priv(drv_data,
  1458. config->reg_capabilities);
  1459. tmp &= LPSS_CAPS_CS_EN_MASK;
  1460. tmp >>= LPSS_CAPS_CS_EN_SHIFT;
  1461. platform_info->num_chipselect = ffz(tmp);
  1462. } else if (config->cs_num) {
  1463. platform_info->num_chipselect = config->cs_num;
  1464. }
  1465. }
  1466. master->num_chipselect = platform_info->num_chipselect;
  1467. count = gpiod_count(&pdev->dev, "cs");
  1468. if (count > 0) {
  1469. int i;
  1470. master->num_chipselect = max_t(int, count,
  1471. master->num_chipselect);
  1472. drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
  1473. master->num_chipselect, sizeof(struct gpio_desc *),
  1474. GFP_KERNEL);
  1475. if (!drv_data->cs_gpiods) {
  1476. status = -ENOMEM;
  1477. goto out_error_clock_enabled;
  1478. }
  1479. for (i = 0; i < master->num_chipselect; i++) {
  1480. struct gpio_desc *gpiod;
  1481. gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
  1482. if (IS_ERR(gpiod)) {
  1483. /* Means use native chip select */
  1484. if (PTR_ERR(gpiod) == -ENOENT)
  1485. continue;
  1486. status = (int)PTR_ERR(gpiod);
  1487. goto out_error_clock_enabled;
  1488. } else {
  1489. drv_data->cs_gpiods[i] = gpiod;
  1490. }
  1491. }
  1492. }
  1493. pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
  1494. pm_runtime_use_autosuspend(&pdev->dev);
  1495. pm_runtime_set_active(&pdev->dev);
  1496. pm_runtime_enable(&pdev->dev);
  1497. /* Register with the SPI framework */
  1498. platform_set_drvdata(pdev, drv_data);
  1499. status = devm_spi_register_controller(&pdev->dev, master);
  1500. if (status != 0) {
  1501. dev_err(&pdev->dev, "problem registering spi master\n");
  1502. goto out_error_clock_enabled;
  1503. }
  1504. return status;
  1505. out_error_clock_enabled:
  1506. pm_runtime_put_noidle(&pdev->dev);
  1507. pm_runtime_disable(&pdev->dev);
  1508. clk_disable_unprepare(ssp->clk);
  1509. out_error_dma_irq_alloc:
  1510. pxa2xx_spi_dma_release(drv_data);
  1511. free_irq(ssp->irq, drv_data);
  1512. out_error_master_alloc:
  1513. spi_controller_put(master);
  1514. pxa_ssp_free(ssp);
  1515. return status;
  1516. }
  1517. static int pxa2xx_spi_remove(struct platform_device *pdev)
  1518. {
  1519. struct driver_data *drv_data = platform_get_drvdata(pdev);
  1520. struct ssp_device *ssp;
  1521. if (!drv_data)
  1522. return 0;
  1523. ssp = drv_data->ssp;
  1524. pm_runtime_get_sync(&pdev->dev);
  1525. /* Disable the SSP at the peripheral and SOC level */
  1526. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1527. clk_disable_unprepare(ssp->clk);
  1528. /* Release DMA */
  1529. if (drv_data->master_info->enable_dma)
  1530. pxa2xx_spi_dma_release(drv_data);
  1531. pm_runtime_put_noidle(&pdev->dev);
  1532. pm_runtime_disable(&pdev->dev);
  1533. /* Release IRQ */
  1534. free_irq(ssp->irq, drv_data);
  1535. /* Release SSP */
  1536. pxa_ssp_free(ssp);
  1537. return 0;
  1538. }
  1539. static void pxa2xx_spi_shutdown(struct platform_device *pdev)
  1540. {
  1541. int status = 0;
  1542. if ((status = pxa2xx_spi_remove(pdev)) != 0)
  1543. dev_err(&pdev->dev, "shutdown failed with %d\n", status);
  1544. }
  1545. #ifdef CONFIG_PM_SLEEP
  1546. static int pxa2xx_spi_suspend(struct device *dev)
  1547. {
  1548. struct driver_data *drv_data = dev_get_drvdata(dev);
  1549. struct ssp_device *ssp = drv_data->ssp;
  1550. int status;
  1551. status = spi_controller_suspend(drv_data->master);
  1552. if (status != 0)
  1553. return status;
  1554. pxa2xx_spi_write(drv_data, SSCR0, 0);
  1555. if (!pm_runtime_suspended(dev))
  1556. clk_disable_unprepare(ssp->clk);
  1557. return 0;
  1558. }
  1559. static int pxa2xx_spi_resume(struct device *dev)
  1560. {
  1561. struct driver_data *drv_data = dev_get_drvdata(dev);
  1562. struct ssp_device *ssp = drv_data->ssp;
  1563. int status;
  1564. /* Enable the SSP clock */
  1565. if (!pm_runtime_suspended(dev)) {
  1566. status = clk_prepare_enable(ssp->clk);
  1567. if (status)
  1568. return status;
  1569. }
  1570. /* Restore LPSS private register bits */
  1571. if (is_lpss_ssp(drv_data))
  1572. lpss_ssp_setup(drv_data);
  1573. /* Start the queue running */
  1574. status = spi_controller_resume(drv_data->master);
  1575. if (status != 0) {
  1576. dev_err(dev, "problem starting queue (%d)\n", status);
  1577. return status;
  1578. }
  1579. return 0;
  1580. }
  1581. #endif
  1582. #ifdef CONFIG_PM
  1583. static int pxa2xx_spi_runtime_suspend(struct device *dev)
  1584. {
  1585. struct driver_data *drv_data = dev_get_drvdata(dev);
  1586. clk_disable_unprepare(drv_data->ssp->clk);
  1587. return 0;
  1588. }
  1589. static int pxa2xx_spi_runtime_resume(struct device *dev)
  1590. {
  1591. struct driver_data *drv_data = dev_get_drvdata(dev);
  1592. int status;
  1593. status = clk_prepare_enable(drv_data->ssp->clk);
  1594. return status;
  1595. }
  1596. #endif
  1597. static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
  1598. SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
  1599. SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
  1600. pxa2xx_spi_runtime_resume, NULL)
  1601. };
  1602. static struct platform_driver driver = {
  1603. .driver = {
  1604. .name = "pxa2xx-spi",
  1605. .pm = &pxa2xx_spi_pm_ops,
  1606. .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
  1607. },
  1608. .probe = pxa2xx_spi_probe,
  1609. .remove = pxa2xx_spi_remove,
  1610. .shutdown = pxa2xx_spi_shutdown,
  1611. };
  1612. static int __init pxa2xx_spi_init(void)
  1613. {
  1614. return platform_driver_register(&driver);
  1615. }
  1616. subsys_initcall(pxa2xx_spi_init);
  1617. static void __exit pxa2xx_spi_exit(void)
  1618. {
  1619. platform_driver_unregister(&driver);
  1620. }
  1621. module_exit(pxa2xx_spi_exit);