blob: 8b44b71f502445c0efee94e8b2d2ea750886caad [file] [log] [blame]
Magnus Damm37e46642008-02-06 01:38:15 -08001/*
2 * SH SCI SPI interface
3 *
4 * Copyright (c) 2008 Magnus Damm
5 *
6 * Based on S3C24XX GPIO based SPI driver, which is:
7 * Copyright (c) 2006 Ben Dooks
8 * Copyright (c) 2006 Simtec Electronics
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/kernel.h>
Magnus Damm37e46642008-02-06 01:38:15 -080017#include <linux/delay.h>
18#include <linux/spinlock.h>
19#include <linux/workqueue.h>
20#include <linux/platform_device.h>
21
22#include <linux/spi/spi.h>
23#include <linux/spi/spi_bitbang.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040024#include <linux/module.h>
Magnus Damm37e46642008-02-06 01:38:15 -080025
26#include <asm/spi.h>
27#include <asm/io.h>
28
29struct sh_sci_spi {
30 struct spi_bitbang bitbang;
31
32 void __iomem *membase;
33 unsigned char val;
34 struct sh_spi_info *info;
35 struct platform_device *dev;
36};
37
38#define SCSPTR(sp) (sp->membase + 0x1c)
39#define PIN_SCK (1 << 2)
40#define PIN_TXD (1 << 0)
41#define PIN_RXD PIN_TXD
42#define PIN_INIT ((1 << 1) | (1 << 3) | PIN_SCK | PIN_TXD)
43
44static inline void setbits(struct sh_sci_spi *sp, int bits, int on)
45{
46 /*
47 * We are the only user of SCSPTR so no locking is required.
48 * Reading bit 2 and 0 in SCSPTR gives pin state as input.
49 * Writing the same bits sets the output value.
50 * This makes regular read-modify-write difficult so we
51 * use sp->val to keep track of the latest register value.
52 */
53
54 if (on)
55 sp->val |= bits;
56 else
57 sp->val &= ~bits;
58
59 iowrite8(sp->val, SCSPTR(sp));
60}
61
62static inline void setsck(struct spi_device *dev, int on)
63{
64 setbits(spi_master_get_devdata(dev->master), PIN_SCK, on);
65}
66
67static inline void setmosi(struct spi_device *dev, int on)
68{
69 setbits(spi_master_get_devdata(dev->master), PIN_TXD, on);
70}
71
72static inline u32 getmiso(struct spi_device *dev)
73{
74 struct sh_sci_spi *sp = spi_master_get_devdata(dev->master);
75
76 return (ioread8(SCSPTR(sp)) & PIN_RXD) ? 1 : 0;
77}
78
79#define spidelay(x) ndelay(x)
80
Grant Likelyca632f52011-06-06 01:16:30 -060081#include "spi-bitbang-txrx.h"
Magnus Damm37e46642008-02-06 01:38:15 -080082
83static u32 sh_sci_spi_txrx_mode0(struct spi_device *spi,
84 unsigned nsecs, u32 word, u8 bits)
85{
Marek Szyprowski04bb2a02010-06-30 14:27:32 -060086 return bitbang_txrx_be_cpha0(spi, nsecs, 0, 0, word, bits);
Magnus Damm37e46642008-02-06 01:38:15 -080087}
88
89static u32 sh_sci_spi_txrx_mode1(struct spi_device *spi,
90 unsigned nsecs, u32 word, u8 bits)
91{
Marek Szyprowski04bb2a02010-06-30 14:27:32 -060092 return bitbang_txrx_be_cpha1(spi, nsecs, 0, 0, word, bits);
Magnus Damm37e46642008-02-06 01:38:15 -080093}
94
95static u32 sh_sci_spi_txrx_mode2(struct spi_device *spi,
96 unsigned nsecs, u32 word, u8 bits)
97{
Marek Szyprowski04bb2a02010-06-30 14:27:32 -060098 return bitbang_txrx_be_cpha0(spi, nsecs, 1, 0, word, bits);
Magnus Damm37e46642008-02-06 01:38:15 -080099}
100
101static u32 sh_sci_spi_txrx_mode3(struct spi_device *spi,
102 unsigned nsecs, u32 word, u8 bits)
103{
Marek Szyprowski04bb2a02010-06-30 14:27:32 -0600104 return bitbang_txrx_be_cpha1(spi, nsecs, 1, 0, word, bits);
Magnus Damm37e46642008-02-06 01:38:15 -0800105}
106
107static void sh_sci_spi_chipselect(struct spi_device *dev, int value)
108{
109 struct sh_sci_spi *sp = spi_master_get_devdata(dev->master);
110
Axel Lined8eb252014-03-13 10:32:37 +0800111 if (sp->info->chip_select)
Magnus Damm37e46642008-02-06 01:38:15 -0800112 (sp->info->chip_select)(sp->info, dev->chip_select, value);
113}
114
115static int sh_sci_spi_probe(struct platform_device *dev)
116{
117 struct resource *r;
118 struct spi_master *master;
119 struct sh_sci_spi *sp;
120 int ret;
121
122 master = spi_alloc_master(&dev->dev, sizeof(struct sh_sci_spi));
123 if (master == NULL) {
124 dev_err(&dev->dev, "failed to allocate spi master\n");
125 ret = -ENOMEM;
126 goto err0;
127 }
128
129 sp = spi_master_get_devdata(master);
130
131 platform_set_drvdata(dev, sp);
Jingoo Han8074cf02013-07-30 16:58:59 +0900132 sp->info = dev_get_platdata(&dev->dev);
Axel Lined8eb252014-03-13 10:32:37 +0800133 if (!sp->info) {
134 dev_err(&dev->dev, "platform data is missing\n");
135 ret = -ENOENT;
136 goto err1;
137 }
Magnus Damm37e46642008-02-06 01:38:15 -0800138
139 /* setup spi bitbang adaptor */
Axel Lin94c69f72013-09-10 15:43:41 +0800140 sp->bitbang.master = master;
Magnus Damm37e46642008-02-06 01:38:15 -0800141 sp->bitbang.master->bus_num = sp->info->bus_num;
142 sp->bitbang.master->num_chipselect = sp->info->num_chipselect;
143 sp->bitbang.chipselect = sh_sci_spi_chipselect;
144
145 sp->bitbang.txrx_word[SPI_MODE_0] = sh_sci_spi_txrx_mode0;
146 sp->bitbang.txrx_word[SPI_MODE_1] = sh_sci_spi_txrx_mode1;
147 sp->bitbang.txrx_word[SPI_MODE_2] = sh_sci_spi_txrx_mode2;
148 sp->bitbang.txrx_word[SPI_MODE_3] = sh_sci_spi_txrx_mode3;
149
150 r = platform_get_resource(dev, IORESOURCE_MEM, 0);
151 if (r == NULL) {
152 ret = -ENOENT;
153 goto err1;
154 }
hartleys76b6fdd2009-12-14 22:40:05 +0000155 sp->membase = ioremap(r->start, resource_size(r));
Magnus Damm37e46642008-02-06 01:38:15 -0800156 if (!sp->membase) {
157 ret = -ENXIO;
158 goto err1;
159 }
160 sp->val = ioread8(SCSPTR(sp));
161 setbits(sp, PIN_INIT, 1);
162
163 ret = spi_bitbang_start(&sp->bitbang);
164 if (!ret)
165 return 0;
166
167 setbits(sp, PIN_INIT, 0);
168 iounmap(sp->membase);
169 err1:
170 spi_master_put(sp->bitbang.master);
171 err0:
172 return ret;
173}
174
175static int sh_sci_spi_remove(struct platform_device *dev)
176{
177 struct sh_sci_spi *sp = platform_get_drvdata(dev);
178
179 iounmap(sp->membase);
180 setbits(sp, PIN_INIT, 0);
181 spi_bitbang_stop(&sp->bitbang);
182 spi_master_put(sp->bitbang.master);
183 return 0;
184}
185
186static struct platform_driver sh_sci_spi_drv = {
187 .probe = sh_sci_spi_probe,
188 .remove = sh_sci_spi_remove,
189 .driver = {
190 .name = "spi_sh_sci",
191 .owner = THIS_MODULE,
192 },
193};
Grant Likely940ab882011-10-05 11:29:49 -0600194module_platform_driver(sh_sci_spi_drv);
Magnus Damm37e46642008-02-06 01:38:15 -0800195
196MODULE_DESCRIPTION("SH SCI SPI Driver");
197MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
198MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -0700199MODULE_ALIAS("platform:spi_sh_sci");