2 * Technologic Systems TS-73xx SBC FPGA loader
4 * Copyright (C) 2016 Florian Fainelli <f.fainelli@gmail.com>
6 * FPGA Manager Driver for the on-board Altera Cyclone II FPGA found on
7 * TS-7300, heavily based on load_fpga.c in their vendor tree.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/string.h>
24 #include <linux/iopoll.h>
25 #include <linux/fpga/fpga-mgr.h>
27 #define TS73XX_FPGA_DATA_REG 0
28 #define TS73XX_FPGA_CONFIG_REG 1
30 #define TS73XX_FPGA_WRITE_DONE 0x1
31 #define TS73XX_FPGA_WRITE_DONE_TIMEOUT 1000 /* us */
32 #define TS73XX_FPGA_RESET 0x2
33 #define TS73XX_FPGA_RESET_LOW_DELAY 30 /* us */
34 #define TS73XX_FPGA_RESET_HIGH_DELAY 80 /* us */
35 #define TS73XX_FPGA_LOAD_OK 0x4
36 #define TS73XX_FPGA_CONFIG_LOAD 0x8
38 struct ts73xx_fpga_priv
{
39 void __iomem
*io_base
;
43 static enum fpga_mgr_states
ts73xx_fpga_state(struct fpga_manager
*mgr
)
45 return FPGA_MGR_STATE_UNKNOWN
;
48 static int ts73xx_fpga_write_init(struct fpga_manager
*mgr
,
49 struct fpga_image_info
*info
,
50 const char *buf
, size_t count
)
52 struct ts73xx_fpga_priv
*priv
= mgr
->priv
;
55 writeb(0, priv
->io_base
+ TS73XX_FPGA_CONFIG_REG
);
56 udelay(TS73XX_FPGA_RESET_LOW_DELAY
);
57 writeb(TS73XX_FPGA_RESET
, priv
->io_base
+ TS73XX_FPGA_CONFIG_REG
);
58 udelay(TS73XX_FPGA_RESET_HIGH_DELAY
);
63 static int ts73xx_fpga_write(struct fpga_manager
*mgr
, const char *buf
,
66 struct ts73xx_fpga_priv
*priv
= mgr
->priv
;
72 ret
= readb_poll_timeout(priv
->io_base
+ TS73XX_FPGA_CONFIG_REG
,
73 reg
, !(reg
& TS73XX_FPGA_WRITE_DONE
),
74 1, TS73XX_FPGA_WRITE_DONE_TIMEOUT
);
78 writeb(buf
[i
], priv
->io_base
+ TS73XX_FPGA_DATA_REG
);
85 static int ts73xx_fpga_write_complete(struct fpga_manager
*mgr
,
86 struct fpga_image_info
*info
)
88 struct ts73xx_fpga_priv
*priv
= mgr
->priv
;
91 usleep_range(1000, 2000);
92 reg
= readb(priv
->io_base
+ TS73XX_FPGA_CONFIG_REG
);
93 reg
|= TS73XX_FPGA_CONFIG_LOAD
;
94 writeb(reg
, priv
->io_base
+ TS73XX_FPGA_CONFIG_REG
);
96 usleep_range(1000, 2000);
97 reg
= readb(priv
->io_base
+ TS73XX_FPGA_CONFIG_REG
);
98 reg
&= ~TS73XX_FPGA_CONFIG_LOAD
;
99 writeb(reg
, priv
->io_base
+ TS73XX_FPGA_CONFIG_REG
);
101 reg
= readb(priv
->io_base
+ TS73XX_FPGA_CONFIG_REG
);
102 if ((reg
& TS73XX_FPGA_LOAD_OK
) != TS73XX_FPGA_LOAD_OK
)
108 static const struct fpga_manager_ops ts73xx_fpga_ops
= {
109 .state
= ts73xx_fpga_state
,
110 .write_init
= ts73xx_fpga_write_init
,
111 .write
= ts73xx_fpga_write
,
112 .write_complete
= ts73xx_fpga_write_complete
,
115 static int ts73xx_fpga_probe(struct platform_device
*pdev
)
117 struct device
*kdev
= &pdev
->dev
;
118 struct ts73xx_fpga_priv
*priv
;
119 struct fpga_manager
*mgr
;
120 struct resource
*res
;
123 priv
= devm_kzalloc(kdev
, sizeof(*priv
), GFP_KERNEL
);
129 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
130 priv
->io_base
= devm_ioremap_resource(kdev
, res
);
131 if (IS_ERR(priv
->io_base
)) {
132 dev_err(kdev
, "unable to remap registers\n");
133 return PTR_ERR(priv
->io_base
);
136 mgr
= fpga_mgr_create(kdev
, "TS-73xx FPGA Manager",
137 &ts73xx_fpga_ops
, priv
);
141 platform_set_drvdata(pdev
, mgr
);
143 ret
= fpga_mgr_register(mgr
);
150 static int ts73xx_fpga_remove(struct platform_device
*pdev
)
152 struct fpga_manager
*mgr
= platform_get_drvdata(pdev
);
154 fpga_mgr_unregister(mgr
);
159 static struct platform_driver ts73xx_fpga_driver
= {
161 .name
= "ts73xx-fpga-mgr",
163 .probe
= ts73xx_fpga_probe
,
164 .remove
= ts73xx_fpga_remove
,
166 module_platform_driver(ts73xx_fpga_driver
);
168 MODULE_AUTHOR("Florian Fainelli <f.fainelli@gmail.com>");
169 MODULE_DESCRIPTION("TS-73xx FPGA Manager driver");
170 MODULE_LICENSE("GPL v2");