blob: 1135c37dcc59a9236411a8a829ccbc741abce852 [file] [log] [blame]
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Joel Nider5556a852011-10-16 10:52:13 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Joel Nider5bd73f82011-12-14 16:53:30 +020013#include <linux/module.h> /* Just for modules */
14#include <linux/kernel.h> /* Only for KERN_INFO */
15#include <linux/err.h> /* Error macros */
16#include <linux/list.h> /* Linked list */
Joel Nider5556a852011-10-16 10:52:13 +020017#include <linux/cdev.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020018#include <linux/init.h> /* Needed for the macros */
19#include <linux/io.h> /* IO macros */
20#include <linux/device.h> /* Device drivers need this */
21#include <linux/sched.h> /* Externally defined globals */
22#include <linux/pm_runtime.h> /* Runtime power management */
Joel Nider5556a852011-10-16 10:52:13 +020023#include <linux/fs.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020024#include <linux/uaccess.h> /* copy_to_user */
Joel Nider5556a852011-10-16 10:52:13 +020025#include <linux/slab.h> /* kfree, kzalloc */
Joel Nider5bd73f82011-12-14 16:53:30 +020026#include <linux/ioport.h> /* XXX_ mem_region */
27#include <linux/dma-mapping.h> /* dma_XXX */
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +020028#include <linux/dmapool.h> /* DMA pools */
Joel Nider5bd73f82011-12-14 16:53:30 +020029#include <linux/delay.h> /* msleep */
30#include <linux/platform_device.h>
Joel Nider5556a852011-10-16 10:52:13 +020031#include <linux/clk.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020032#include <linux/poll.h> /* poll() file op */
33#include <linux/wait.h> /* wait() macros, sleeping */
34#include <linux/tspp.h> /* tspp functions */
Joel Nider5556a852011-10-16 10:52:13 +020035#include <linux/bitops.h> /* BIT() macro */
Joel Nider5bd73f82011-12-14 16:53:30 +020036#include <mach/sps.h> /* BAM stuff */
Joel Nider5556a852011-10-16 10:52:13 +020037#include <mach/gpio.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020038#include <linux/wakelock.h> /* Locking functions */
Hamad Kadmany567bed82012-11-29 14:15:57 +020039#include <linux/timer.h> /* Timer services */
40#include <linux/jiffies.h> /* Jiffies counter */
Joel Nider5556a852011-10-16 10:52:13 +020041#include <mach/dma.h>
42#include <mach/msm_tspp.h>
Joel Nider5556a852011-10-16 10:52:13 +020043#include <linux/debugfs.h>
Liron Kuch8fa85b02013-01-01 18:29:47 +020044#include <linux/of.h>
45#include <linux/of_gpio.h>
Liron Kuch65067fa2013-02-10 15:19:32 +020046#include <linux/string.h>
Joel Nider5556a852011-10-16 10:52:13 +020047
48/*
49 * General defines
50 */
Joel Nider5556a852011-10-16 10:52:13 +020051#define TSPP_TSIF_INSTANCES 2
Liron Kuch65067fa2013-02-10 15:19:32 +020052#define TSPP_GPIOS_PER_TSIF 4
Joel Nider5556a852011-10-16 10:52:13 +020053#define TSPP_FILTER_TABLES 3
Joel Nider5bd73f82011-12-14 16:53:30 +020054#define TSPP_MAX_DEVICES 1
Joel Nider5556a852011-10-16 10:52:13 +020055#define TSPP_NUM_CHANNELS 16
56#define TSPP_NUM_PRIORITIES 16
57#define TSPP_NUM_KEYS 8
58#define INVALID_CHANNEL 0xFFFFFFFF
Hamad Kadmany567bed82012-11-29 14:15:57 +020059
60/*
61 * BAM descriptor FIFO size (in number of descriptors).
62 * Max number of descriptors allowed by SPS which is 8K-1.
Hamad Kadmany567bed82012-11-29 14:15:57 +020063 */
Hamad Kadmany45f3f062013-05-22 15:40:38 +030064#define TSPP_SPS_DESCRIPTOR_COUNT (8 * 1024 - 1)
Joel Nider5556a852011-10-16 10:52:13 +020065#define TSPP_PACKET_LENGTH 188
66#define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH)
Hamad Kadmany567bed82012-11-29 14:15:57 +020067
68/* Max descriptor buffer size allowed by SPS */
69#define TSPP_MAX_BUFFER_SIZE (32 * 1024 - 1)
70
71/*
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +020072 * Returns whether to use DMA pool for TSPP output buffers.
73 * For buffers smaller than page size, using DMA pool
74 * provides better memory utilization as dma_alloc_coherent
75 * allocates minimum of page size.
76 */
77#define TSPP_USE_DMA_POOL(buff_size) ((buff_size) < PAGE_SIZE)
78
79/*
Hamad Kadmany567bed82012-11-29 14:15:57 +020080 * Max allowed TSPP buffers/descriptors.
81 * If SPS desc FIFO holds X descriptors, we can queue up to X-1 descriptors.
82 */
83#define TSPP_NUM_BUFFERS (TSPP_SPS_DESCRIPTOR_COUNT - 1)
Joel Nider5556a852011-10-16 10:52:13 +020084#define TSPP_TSIF_DEFAULT_TIME_LIMIT 60
85#define SPS_DESCRIPTOR_SIZE 8
86#define MIN_ACCEPTABLE_BUFFER_COUNT 2
Joel Nider5bd73f82011-12-14 16:53:30 +020087#define TSPP_DEBUG(msg...)
Joel Nider5556a852011-10-16 10:52:13 +020088
89/*
90 * TSIF register offsets
91 */
92#define TSIF_STS_CTL_OFF (0x0)
93#define TSIF_TIME_LIMIT_OFF (0x4)
94#define TSIF_CLK_REF_OFF (0x8)
95#define TSIF_LPBK_FLAGS_OFF (0xc)
96#define TSIF_LPBK_DATA_OFF (0x10)
97#define TSIF_TEST_CTL_OFF (0x14)
98#define TSIF_TEST_MODE_OFF (0x18)
99#define TSIF_TEST_RESET_OFF (0x1c)
100#define TSIF_TEST_EXPORT_OFF (0x20)
101#define TSIF_TEST_CURRENT_OFF (0x24)
102
103#define TSIF_DATA_PORT_OFF (0x100)
104
105/* bits for TSIF_STS_CTL register */
106#define TSIF_STS_CTL_EN_IRQ BIT(28)
107#define TSIF_STS_CTL_PACK_AVAIL BIT(27)
108#define TSIF_STS_CTL_1ST_PACKET BIT(26)
109#define TSIF_STS_CTL_OVERFLOW BIT(25)
110#define TSIF_STS_CTL_LOST_SYNC BIT(24)
111#define TSIF_STS_CTL_TIMEOUT BIT(23)
112#define TSIF_STS_CTL_INV_SYNC BIT(21)
113#define TSIF_STS_CTL_INV_NULL BIT(20)
114#define TSIF_STS_CTL_INV_ERROR BIT(19)
115#define TSIF_STS_CTL_INV_ENABLE BIT(18)
116#define TSIF_STS_CTL_INV_DATA BIT(17)
117#define TSIF_STS_CTL_INV_CLOCK BIT(16)
118#define TSIF_STS_CTL_SPARE BIT(15)
119#define TSIF_STS_CTL_EN_NULL BIT(11)
120#define TSIF_STS_CTL_EN_ERROR BIT(10)
121#define TSIF_STS_CTL_LAST_BIT BIT(9)
122#define TSIF_STS_CTL_EN_TIME_LIM BIT(8)
123#define TSIF_STS_CTL_EN_TCR BIT(7)
124#define TSIF_STS_CTL_TEST_MODE BIT(6)
Joel Nider5bd73f82011-12-14 16:53:30 +0200125#define TSIF_STS_CTL_MODE_2 BIT(5)
Joel Nider5556a852011-10-16 10:52:13 +0200126#define TSIF_STS_CTL_EN_DM BIT(4)
127#define TSIF_STS_CTL_STOP BIT(3)
128#define TSIF_STS_CTL_START BIT(0)
129
130/*
131 * TSPP register offsets
132 */
Liron Kuch229090d2012-10-30 17:47:50 +0200133#define TSPP_RST 0x00
Joel Nider5556a852011-10-16 10:52:13 +0200134#define TSPP_CLK_CONTROL 0x04
Liron Kuch229090d2012-10-30 17:47:50 +0200135#define TSPP_CONFIG 0x08
136#define TSPP_CONTROL 0x0C
Joel Nider5556a852011-10-16 10:52:13 +0200137#define TSPP_PS_DISABLE 0x10
Liron Kuch229090d2012-10-30 17:47:50 +0200138#define TSPP_MSG_IRQ_STATUS 0x14
Joel Nider5556a852011-10-16 10:52:13 +0200139#define TSPP_MSG_IRQ_MASK 0x18
140#define TSPP_IRQ_STATUS 0x1C
141#define TSPP_IRQ_MASK 0x20
142#define TSPP_IRQ_CLEAR 0x24
143#define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2))
Liron Kuch229090d2012-10-30 17:47:50 +0200144#define TSPP_STATUS 0x68
145#define TSPP_CURR_TSP_HEADER 0x6C
146#define TSPP_CURR_PID_FILTER 0x70
147#define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2))
148#define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2))
149#define TSPP_DATA_KEY_RESET 0x9C
Joel Nider5556a852011-10-16 10:52:13 +0200150#define TSPP_KEY_VALID 0xA0
151#define TSPP_KEY_ERROR 0xA4
152#define TSPP_TEST_CTRL 0xA8
Liron Kuch229090d2012-10-30 17:47:50 +0200153#define TSPP_VERSION 0xAC
Joel Nider5556a852011-10-16 10:52:13 +0200154#define TSPP_GENERICS 0xB0
Liron Kuch229090d2012-10-30 17:47:50 +0200155#define TSPP_NOP 0xB4
Joel Nider5556a852011-10-16 10:52:13 +0200156
157/*
158 * Register bit definitions
159 */
160/* TSPP_RST */
161#define TSPP_RST_RESET BIT(0)
162
163/* TSPP_CLK_CONTROL */
164#define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9)
165#define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8)
166#define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7)
167#define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6)
168#define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5)
169#define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4)
170#define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3)
171#define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2)
172#define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1)
173#define TSPP_CLK_CONTROL_SET_CLKON BIT(0)
174
175/* TSPP_CONFIG */
176#define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \
177((_b & 0xF) << 8))
178#define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF)
179#define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7)
180#define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6)
181#define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5)
182#define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4)
183#define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3)
184#define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2)
185#define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1)
186#define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0)
187
188/* TSPP_CONTROL */
189#define TSPP_CONTROL_PID_FILTER_LOCK BIT(5)
190#define TSPP_CONTROL_FORCE_KEY_CALC BIT(4)
191#define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3)
192#define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2)
193#define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1)
194#define TSPP_CONTROL_PERF_COUNT_INIT BIT(0)
195
196/* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */
197#define TSPP_MSG_TSPP_IRQ BIT(2)
198#define TSPP_MSG_TSIF_1_IRQ BIT(1)
199#define TSPP_MSG_TSIF_0_IRQ BIT(0)
200
201/* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */
Liron Kuch229090d2012-10-30 17:47:50 +0200202#define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19)
203#define TSPP_IRQ_STATUS_KEY_ERROR BIT(18)
Joel Nider5556a852011-10-16 10:52:13 +0200204#define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17)
205#define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16)
206#define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n))
207
208/* TSPP_PIPE_ERROR_STATUS */
Liron Kuch229090d2012-10-30 17:47:50 +0200209#define TSPP_PIPE_PES_SYNC_ERROR BIT(3)
210#define TSPP_PIPE_PS_LENGTH_ERROR BIT(2)
Joel Nider5556a852011-10-16 10:52:13 +0200211#define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1)
Liron Kuch229090d2012-10-30 17:47:50 +0200212#define TSPP_PIP_PS_LOST_START BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200213
214/* TSPP_STATUS */
Liron Kuch229090d2012-10-30 17:47:50 +0200215#define TSPP_STATUS_TSP_PKT_AVAIL BIT(10)
216#define TSPP_STATUS_TSIF1_DM_REQ BIT(6)
217#define TSPP_STATUS_TSIF0_DM_REQ BIT(2)
218#define TSPP_CURR_FILTER_TABLE BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200219
220/* TSPP_GENERICS */
Liron Kuch229090d2012-10-30 17:47:50 +0200221#define TSPP_GENERICS_CRYPTO_GEN BIT(12)
Joel Nider5556a852011-10-16 10:52:13 +0200222#define TSPP_GENERICS_MAX_CONS_PIPES BIT(7)
Liron Kuch229090d2012-10-30 17:47:50 +0200223#define TSPP_GENERICS_MAX_PIPES BIT(2)
224#define TSPP_GENERICS_TSIF_1_GEN BIT(1)
225#define TSPP_GENERICS_TSIF_0_GEN BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200226
227/*
228 * TSPP memory regions
229 */
230#define TSPP_PID_FILTER_TABLE0 0x800
231#define TSPP_PID_FILTER_TABLE1 0x880
232#define TSPP_PID_FILTER_TABLE2 0x900
233#define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */
234#define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */
235#define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */
236#define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2))
237#define TSPP_DATA_KEY 0xCD0
238
Joel Nider5556a852011-10-16 10:52:13 +0200239struct debugfs_entry {
240 const char *name;
241 mode_t mode;
242 int offset;
243};
244
245static const struct debugfs_entry debugfs_tsif_regs[] = {
246 {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF},
247 {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF},
248 {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF},
249 {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF},
250 {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF},
251 {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF},
252 {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF},
253 {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF},
254 {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF},
255 {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF},
256 {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF},
257};
258
259static const struct debugfs_entry debugfs_tspp_regs[] = {
260 {"rst", S_IRUGO | S_IWUSR, TSPP_RST},
261 {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL},
262 {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG},
263 {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL},
264 {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE},
265 {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS},
266 {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK},
267 {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS},
268 {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK},
269 {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR},
270 /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */
271 {"status", S_IRUGO | S_IWUSR, TSPP_STATUS},
272 {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER},
273 {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER},
274 /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */
275 /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */
276 {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET},
277 {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID},
278 {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR},
279 {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL},
280 {"version", S_IRUGO | S_IWUSR, TSPP_VERSION},
281 {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS},
282 {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0},
283 {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1},
284 {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2},
285 {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE},
286 {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT},
287 {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE},
288 {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY}
289};
290
Joel Nider5556a852011-10-16 10:52:13 +0200291struct tspp_pid_filter {
292 u32 filter; /* see FILTER_ macros */
293 u32 config; /* see FILTER_ macros */
294};
295
296/* tsp_info */
297#define FILTER_HEADER_ERROR_MASK BIT(7)
298#define FILTER_TRANS_END_DISABLE BIT(6)
299#define FILTER_DEC_ON_ERROR_EN BIT(5)
300#define FILTER_DECRYPT BIT(4)
301#define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT)
302#define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF)
303#define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \
304 (_p->config & ~0xF) | (_b & 0xF))
305#define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3)
306#define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \
307 (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30))
308#define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF)
309#define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \
310 (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13))
311#define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF)
312#define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \
313 (_p->filter & ~0x1FFF) | (_b & 0x1FFF))
314#define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3)
315#define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \
316 (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30))
317#define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7)
318#define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \
319 (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8))
320
321struct tspp_global_performance_regs {
322 u32 tsp_total;
323 u32 tsp_ignored;
324 u32 tsp_error;
325 u32 tsp_sync;
326};
327
328struct tspp_pipe_context_regs {
329 u16 pes_bytes_left;
330 u16 count;
331 u32 tsif_suffix;
332} __packed;
333#define CONTEXT_GET_STATE(_a) (_a & 0x3)
334#define CONTEXT_UNSPEC_LENGTH BIT(11)
335#define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF)
336
Hamad Kadmany567bed82012-11-29 14:15:57 +0200337#define MSEC_TO_JIFFIES(msec) ((msec) * HZ / 1000)
338
Joel Nider5556a852011-10-16 10:52:13 +0200339struct tspp_pipe_performance_regs {
340 u32 tsp_total;
341 u32 ps_duplicate_tsp;
342 u32 tsp_no_payload;
343 u32 tsp_broken_ps;
344 u32 ps_total_num;
345 u32 ps_continuity_error;
346 u32 ps_length_error;
347 u32 pes_sync_error;
348};
349
350struct tspp_tsif_device {
351 void __iomem *base;
352 u32 time_limit;
353 u32 ref_count;
Joel Nider5bd73f82011-12-14 16:53:30 +0200354 enum tspp_tsif_mode mode;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200355 int clock_inverse;
356 int data_inverse;
357 int sync_inverse;
358 int enable_inverse;
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200359 u32 tsif_irq;
Joel Nider5556a852011-10-16 10:52:13 +0200360
361 /* debugfs */
Joel Nider5556a852011-10-16 10:52:13 +0200362 struct dentry *dent_tsif;
363 struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)];
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200364 u32 stat_rx;
365 u32 stat_overflow;
366 u32 stat_lost_sync;
367 u32 stat_timeout;
Joel Nider5556a852011-10-16 10:52:13 +0200368};
369
370enum tspp_buf_state {
371 TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */
372 TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */
Joel Nider5bd73f82011-12-14 16:53:30 +0200373 TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */
374 TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */
Joel Nider5556a852011-10-16 10:52:13 +0200375};
376
377struct tspp_mem_buffer {
Joel Nider5bd73f82011-12-14 16:53:30 +0200378 struct tspp_mem_buffer *next;
379 struct sps_mem_buffer sps;
380 struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */
Joel Nider5556a852011-10-16 10:52:13 +0200381 enum tspp_buf_state state;
382 size_t filled; /* how much data this buffer is holding */
383 int read_index; /* where to start reading data from */
384};
385
386/* this represents each char device 'channel' */
387struct tspp_channel {
388 struct cdev cdev;
389 struct device *dd;
Joel Nider5bd73f82011-12-14 16:53:30 +0200390 struct tspp_device *pdev; /* can use container_of instead? */
Joel Nider5556a852011-10-16 10:52:13 +0200391 struct sps_pipe *pipe;
392 struct sps_connect config;
393 struct sps_register_event event;
Joel Nider5bd73f82011-12-14 16:53:30 +0200394 struct tspp_mem_buffer *data; /* list of buffers */
395 struct tspp_mem_buffer *read; /* first buffer ready to be read */
396 struct tspp_mem_buffer *waiting; /* first outstanding transfer */
397 struct tspp_mem_buffer *locked; /* buffer currently being read */
Joel Nider5556a852011-10-16 10:52:13 +0200398 wait_queue_head_t in_queue; /* set when data is received */
Joel Nider5bd73f82011-12-14 16:53:30 +0200399 u32 id; /* channel id (0-15) */
400 int used; /* is this channel in use? */
401 int key; /* which encryption key index is used */
402 u32 buffer_size; /* size of the sps transfer buffers */
403 u32 max_buffers; /* how many buffers should be allocated */
404 u32 buffer_count; /* how many buffers are actually allocated */
405 u32 filter_count; /* how many filters have been added to this channel */
406 u32 int_freq; /* generate interrupts every x descriptors */
Joel Nider5556a852011-10-16 10:52:13 +0200407 enum tspp_source src;
408 enum tspp_mode mode;
Joel Nider5bd73f82011-12-14 16:53:30 +0200409 tspp_notifier *notifier; /* used only with kernel api */
410 void *notify_data; /* data to be passed with the notifier */
Hamad Kadmany567bed82012-11-29 14:15:57 +0200411 u32 expiration_period_ms; /* notification on partially filled buffers */
412 struct timer_list expiration_timer;
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200413 struct dma_pool *dma_pool;
Liron Kuch229090d2012-10-30 17:47:50 +0200414 tspp_memfree *memfree; /* user defined memory free function */
415 void *user_info; /* user cookie passed to memory alloc/free function */
Joel Nider5556a852011-10-16 10:52:13 +0200416};
417
418struct tspp_pid_filter_table {
419 struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES];
420};
421
422struct tspp_key_entry {
423 u32 even_lsb;
424 u32 even_msb;
425 u32 odd_lsb;
426 u32 odd_msb;
427};
428
429struct tspp_key_table {
430 struct tspp_key_entry entry[TSPP_NUM_KEYS];
431};
432
Joel Nider5bd73f82011-12-14 16:53:30 +0200433/* this represents the actual hardware device */
434struct tspp_device {
435 struct list_head devlist; /* list of all devices */
436 struct platform_device *pdev;
437 void __iomem *base;
438 unsigned int tspp_irq;
439 unsigned int bam_irq;
440 u32 bam_handle;
441 struct sps_bam_props bam_props;
442 struct wake_lock wake_lock;
443 spinlock_t spinlock;
444 struct tasklet_struct tlet;
445 struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES];
446 /* clocks */
447 struct clk *tsif_pclk;
448 struct clk *tsif_ref_clk;
449 /* data */
450 struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES];
451 struct tspp_channel channels[TSPP_NUM_CHANNELS];
452 struct tspp_key_table *tspp_key_table;
453 struct tspp_global_performance_regs *tspp_global_performance;
454 struct tspp_pipe_context_regs *tspp_pipe_context;
455 struct tspp_pipe_performance_regs *tspp_pipe_performance;
456
457 struct dentry *dent;
458 struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)];
459};
460
461
Joel Nider5556a852011-10-16 10:52:13 +0200462static struct class *tspp_class;
463static int tspp_key_entry;
464static dev_t tspp_minor; /* next minor number to assign */
Joel Nider5bd73f82011-12-14 16:53:30 +0200465
466static LIST_HEAD(tspp_devices);
467
468/* forward declarations */
469static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *);
470static ssize_t tspp_open(struct inode *inode, struct file *filp);
471static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p);
472static ssize_t tspp_release(struct inode *inode, struct file *filp);
473static long tspp_ioctl(struct file *, unsigned int, unsigned long);
474
475/* file operations */
476static const struct file_operations tspp_fops = {
477 .owner = THIS_MODULE,
478 .read = tspp_read,
479 .open = tspp_open,
480 .poll = tspp_poll,
481 .release = tspp_release,
482 .unlocked_ioctl = tspp_ioctl,
483};
Joel Nider5556a852011-10-16 10:52:13 +0200484
485/*** IRQ ***/
Joel Nider5bd73f82011-12-14 16:53:30 +0200486static irqreturn_t tspp_isr(int irq, void *dev)
Joel Nider5556a852011-10-16 10:52:13 +0200487{
Joel Nider5bd73f82011-12-14 16:53:30 +0200488 struct tspp_device *device = dev;
Joel Nider5556a852011-10-16 10:52:13 +0200489 u32 status, mask;
490 u32 data;
491
492 status = readl_relaxed(device->base + TSPP_IRQ_STATUS);
493 mask = readl_relaxed(device->base + TSPP_IRQ_MASK);
494 status &= mask;
495
496 if (!status) {
497 dev_warn(&device->pdev->dev, "Spurious interrupt");
498 return IRQ_NONE;
499 }
500
501 /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */
502
503 if (status & TSPP_IRQ_STATUS_KEY_ERROR) {
504 /* read the key error info */
505 data = readl_relaxed(device->base + TSPP_KEY_ERROR);
506 dev_info(&device->pdev->dev, "key error 0x%x", data);
507 }
508 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) {
509 data = readl_relaxed(device->base + TSPP_KEY_VALID);
510 dev_info(&device->pdev->dev, "key invalidated: 0x%x", data);
511 }
512 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED)
513 dev_info(&device->pdev->dev, "key switched");
514
515 if (status & 0xffff)
Joel Nider5bd73f82011-12-14 16:53:30 +0200516 dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff);
Joel Nider5556a852011-10-16 10:52:13 +0200517
518 writel_relaxed(status, device->base + TSPP_IRQ_CLEAR);
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200519
520 /*
521 * Before returning IRQ_HANDLED to the generic interrupt handling
522 * framework need to make sure all operations including clearing of
523 * interrupt status registers in the hardware is performed.
524 * Thus a barrier after clearing the interrupt status register
525 * is required to guarantee that the interrupt status register has
526 * really been cleared by the time we return from this handler.
527 */
528 wmb();
529 return IRQ_HANDLED;
530}
531
532static irqreturn_t tsif_isr(int irq, void *dev)
533{
534 struct tspp_tsif_device *tsif_device = dev;
535 u32 sts_ctl = ioread32(tsif_device->base + TSIF_STS_CTL_OFF);
536
537 if (!(sts_ctl & (TSIF_STS_CTL_PACK_AVAIL |
538 TSIF_STS_CTL_OVERFLOW |
539 TSIF_STS_CTL_LOST_SYNC |
540 TSIF_STS_CTL_TIMEOUT)))
541 return IRQ_NONE;
542
543 if (sts_ctl & TSIF_STS_CTL_OVERFLOW)
544 tsif_device->stat_overflow++;
545
546 if (sts_ctl & TSIF_STS_CTL_LOST_SYNC)
547 tsif_device->stat_lost_sync++;
548
549 if (sts_ctl & TSIF_STS_CTL_TIMEOUT)
550 tsif_device->stat_timeout++;
551
552 iowrite32(sts_ctl, tsif_device->base + TSIF_STS_CTL_OFF);
553
554 /*
555 * Before returning IRQ_HANDLED to the generic interrupt handling
556 * framework need to make sure all operations including clearing of
557 * interrupt status registers in the hardware is performed.
558 * Thus a barrier after clearing the interrupt status register
559 * is required to guarantee that the interrupt status register has
560 * really been cleared by the time we return from this handler.
561 */
Joel Nider5556a852011-10-16 10:52:13 +0200562 wmb();
563 return IRQ_HANDLED;
564}
565
566/*** callbacks ***/
567static void tspp_sps_complete_cb(struct sps_event_notify *notify)
568{
Joel Nider5bd73f82011-12-14 16:53:30 +0200569 struct tspp_device *pdev = notify->user;
570 tasklet_schedule(&pdev->tlet);
Joel Nider5556a852011-10-16 10:52:13 +0200571}
572
Hamad Kadmany567bed82012-11-29 14:15:57 +0200573static void tspp_expiration_timer(unsigned long data)
574{
575 struct tspp_device *pdev = (struct tspp_device *)data;
576
577 if (pdev)
578 tasklet_schedule(&pdev->tlet);
579}
580
Joel Nider5556a852011-10-16 10:52:13 +0200581/*** tasklet ***/
582static void tspp_sps_complete_tlet(unsigned long data)
583{
584 int i;
585 int complete;
586 unsigned long flags;
587 struct sps_iovec iovec;
588 struct tspp_channel *channel;
589 struct tspp_device *device = (struct tspp_device *)data;
Joel Nider5556a852011-10-16 10:52:13 +0200590 spin_lock_irqsave(&device->spinlock, flags);
591
592 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
593 complete = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +0200594 channel = &device->channels[i];
Hamad Kadmany567bed82012-11-29 14:15:57 +0200595
Joel Nider5bd73f82011-12-14 16:53:30 +0200596 if (!channel->used || !channel->waiting)
597 continue;
Joel Nider5556a852011-10-16 10:52:13 +0200598
Hamad Kadmany567bed82012-11-29 14:15:57 +0200599 /* stop the expiration timer */
600 if (channel->expiration_period_ms)
601 del_timer(&channel->expiration_timer);
602
Joel Nider5556a852011-10-16 10:52:13 +0200603 /* get completions */
Joel Nider5bd73f82011-12-14 16:53:30 +0200604 while (channel->waiting->state == TSPP_BUF_STATE_WAITING) {
Joel Nider5556a852011-10-16 10:52:13 +0200605 if (sps_get_iovec(channel->pipe, &iovec) != 0) {
606 pr_err("tspp: Error in iovec on channel %i",
607 channel->id);
608 break;
609 }
610 if (iovec.size == 0)
611 break;
612
Joel Nider5bd73f82011-12-14 16:53:30 +0200613 if (iovec.addr != channel->waiting->sps.phys_base)
Joel Nider5556a852011-10-16 10:52:13 +0200614 pr_err("tspp: buffer mismatch 0x%08x",
Joel Nider5bd73f82011-12-14 16:53:30 +0200615 channel->waiting->sps.phys_base);
Joel Nider5556a852011-10-16 10:52:13 +0200616
617 complete = 1;
Joel Nider5bd73f82011-12-14 16:53:30 +0200618 channel->waiting->state = TSPP_BUF_STATE_DATA;
619 channel->waiting->filled = iovec.size;
620 channel->waiting->read_index = 0;
621
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200622 if (channel->src == TSPP_SOURCE_TSIF0)
623 device->tsif[0].stat_rx++;
624 else if (channel->src == TSPP_SOURCE_TSIF1)
625 device->tsif[1].stat_rx++;
626
Joel Nider5bd73f82011-12-14 16:53:30 +0200627 /* update the pointers */
628 channel->waiting = channel->waiting->next;
Joel Nider5556a852011-10-16 10:52:13 +0200629 }
630
Joel Nider5bd73f82011-12-14 16:53:30 +0200631 /* wake any waiting processes */
Joel Nider5556a852011-10-16 10:52:13 +0200632 if (complete) {
Joel Nider5556a852011-10-16 10:52:13 +0200633 wake_up_interruptible(&channel->in_queue);
Joel Nider5bd73f82011-12-14 16:53:30 +0200634
635 /* call notifiers */
636 if (channel->notifier)
637 channel->notifier(channel->id,
638 channel->notify_data);
Joel Nider5556a852011-10-16 10:52:13 +0200639 }
Hamad Kadmany567bed82012-11-29 14:15:57 +0200640
641 /* restart expiration timer */
642 if (channel->expiration_period_ms)
643 mod_timer(&channel->expiration_timer,
644 jiffies +
645 MSEC_TO_JIFFIES(
646 channel->expiration_period_ms));
Joel Nider5556a852011-10-16 10:52:13 +0200647 }
648
649 spin_unlock_irqrestore(&device->spinlock, flags);
650}
651
652/*** GPIO functions ***/
Liron Kuch65067fa2013-02-10 15:19:32 +0200653static int tspp_gpios_disable(const struct tspp_tsif_device *tsif_device,
654 const struct msm_gpio *table,
655 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200656{
657 int rc = 0;
658 int i;
659 const struct msm_gpio *g;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200660
Joel Nider5556a852011-10-16 10:52:13 +0200661 for (i = size-1; i >= 0; i--) {
662 int tmp;
663 g = table + i;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200664
Liron Kuch65067fa2013-02-10 15:19:32 +0200665 /* don't use sync GPIO when not working in mode 2 */
666 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
667 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
668 continue;
669
Liron Kuch8fa85b02013-01-01 18:29:47 +0200670 tmp = gpio_tlmm_config(GPIO_CFG(GPIO_PIN(g->gpio_cfg),
671 0, GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),
672 GPIO_CFG_DISABLE);
Joel Nider5556a852011-10-16 10:52:13 +0200673 if (tmp) {
Liron Kuch229090d2012-10-30 17:47:50 +0200674 pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200675 g->gpio_cfg, g->label ?: "?", rc);
676 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
677 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
678 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
679 GPIO_DRVSTR(g->gpio_cfg));
680 if (!rc)
681 rc = tmp;
682 }
683 }
684
685 return rc;
686}
687
Liron Kuch65067fa2013-02-10 15:19:32 +0200688static int tspp_gpios_enable(const struct tspp_tsif_device *tsif_device,
689 const struct msm_gpio *table,
690 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200691{
692 int rc;
Liron Kuch65067fa2013-02-10 15:19:32 +0200693 int i;
Joel Nider5556a852011-10-16 10:52:13 +0200694 const struct msm_gpio *g;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200695
Joel Nider5556a852011-10-16 10:52:13 +0200696 for (i = 0; i < size; i++) {
697 g = table + i;
Liron Kuch65067fa2013-02-10 15:19:32 +0200698
699 /* don't use sync GPIO when not working in mode 2 */
700 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
701 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
702 continue;
703
Joel Nider5556a852011-10-16 10:52:13 +0200704 rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
705 if (rc) {
Liron Kuch229090d2012-10-30 17:47:50 +0200706 pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200707 g->gpio_cfg, g->label ?: "?", rc);
708 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
709 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
710 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
711 GPIO_DRVSTR(g->gpio_cfg));
712 goto err;
713 }
714 }
715 return 0;
716err:
Liron Kuch65067fa2013-02-10 15:19:32 +0200717 tspp_gpios_disable(tsif_device, table, i);
Joel Nider5556a852011-10-16 10:52:13 +0200718
Joel Nider5556a852011-10-16 10:52:13 +0200719 return rc;
720}
721
Liron Kuch65067fa2013-02-10 15:19:32 +0200722
723static int tspp_config_gpios(struct tspp_device *device,
724 enum tspp_source source,
725 int enable)
Joel Nider5556a852011-10-16 10:52:13 +0200726{
Liron Kuch65067fa2013-02-10 15:19:32 +0200727 const struct msm_gpio *table;
728 struct msm_tspp_platform_data *pdata = device->pdev->dev.platform_data;
729 int num_gpios = (pdata->num_gpios / TSPP_TSIF_INSTANCES);
730 int i = 0;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200731
Liron Kuch65067fa2013-02-10 15:19:32 +0200732 if (num_gpios != TSPP_GPIOS_PER_TSIF) {
733 pr_err("tspp %s: unexpected number of GPIOs %d, expected %d\n",
734 __func__, num_gpios, TSPP_GPIOS_PER_TSIF);
735 return -EINVAL;
736 }
Joel Nider5556a852011-10-16 10:52:13 +0200737
Liron Kuch65067fa2013-02-10 15:19:32 +0200738 /*
739 * Note: this code assumes that the GPIO definitions in the
740 * pdata->gpios table are according to the TSIF instance number,
741 * i.e., that TSIF0 GPIOs are defined first, then TSIF1 GPIOs etc.
742 */
743 switch (source) {
744 case TSPP_SOURCE_TSIF0:
745 i = 0;
746 break;
747 case TSPP_SOURCE_TSIF1:
748 i = 1;
749 break;
750 default:
751 pr_err("tspp %s: invalid source\n", __func__);
752 return -EINVAL;
753 }
Liron Kuch8fa85b02013-01-01 18:29:47 +0200754
Liron Kuch65067fa2013-02-10 15:19:32 +0200755 table = pdata->gpios + (i * num_gpios);
756 if (enable)
757 return tspp_gpios_enable(&device->tsif[i], table, num_gpios);
758 else
759 return tspp_gpios_disable(&device->tsif[i], table, num_gpios);
Joel Nider5556a852011-10-16 10:52:13 +0200760}
761
Joel Nider5bd73f82011-12-14 16:53:30 +0200762/*** Clock functions ***/
763static int tspp_clock_start(struct tspp_device *device)
764{
Liron Kuchde8cbf92013-02-21 14:25:57 +0200765 if (device == NULL) {
766 pr_err("tspp: Can't start clocks, invalid device\n");
767 return -EINVAL;
768 }
769
Joel Nider5bd73f82011-12-14 16:53:30 +0200770 if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
771 pr_err("tspp: Can't start pclk");
772 return -EBUSY;
773 }
774
775 if (device->tsif_ref_clk &&
776 clk_prepare_enable(device->tsif_ref_clk) != 0) {
777 pr_err("tspp: Can't start ref clk");
778 clk_disable_unprepare(device->tsif_pclk);
779 return -EBUSY;
780 }
781
782 return 0;
783}
784
785static void tspp_clock_stop(struct tspp_device *device)
786{
Liron Kuchde8cbf92013-02-21 14:25:57 +0200787 if (device == NULL) {
788 pr_err("tspp: Can't stop clocks, invalid device\n");
789 return;
790 }
791
Joel Nider5bd73f82011-12-14 16:53:30 +0200792 if (device->tsif_pclk)
Liron Kuchde8cbf92013-02-21 14:25:57 +0200793 clk_disable_unprepare(device->tsif_pclk);
Joel Nider5bd73f82011-12-14 16:53:30 +0200794
795 if (device->tsif_ref_clk)
Liron Kuchde8cbf92013-02-21 14:25:57 +0200796 clk_disable_unprepare(device->tsif_ref_clk);
Joel Nider5bd73f82011-12-14 16:53:30 +0200797}
798
Joel Nider5556a852011-10-16 10:52:13 +0200799/*** TSIF functions ***/
800static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
801{
802 int start_hardware = 0;
803 u32 ctl;
804
805 if (tsif_device->ref_count == 0) {
806 start_hardware = 1;
807 } else if (tsif_device->ref_count > 0) {
808 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
809 if ((ctl & TSIF_STS_CTL_START) != 1) {
810 /* this hardware should already be running */
811 pr_warn("tspp: tsif hw not started but ref count > 0");
812 start_hardware = 1;
813 }
814 }
815
816 if (start_hardware) {
Joel Nider5bd73f82011-12-14 16:53:30 +0200817 ctl = TSIF_STS_CTL_EN_IRQ |
Joel Nider5556a852011-10-16 10:52:13 +0200818 TSIF_STS_CTL_EN_DM;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200819
820 if (tsif_device->clock_inverse)
821 ctl |= TSIF_STS_CTL_INV_CLOCK;
822
823 if (tsif_device->data_inverse)
824 ctl |= TSIF_STS_CTL_INV_DATA;
825
826 if (tsif_device->sync_inverse)
827 ctl |= TSIF_STS_CTL_INV_SYNC;
828
829 if (tsif_device->enable_inverse)
830 ctl |= TSIF_STS_CTL_INV_ENABLE;
831
Joel Nider5bd73f82011-12-14 16:53:30 +0200832 switch (tsif_device->mode) {
833 case TSPP_TSIF_MODE_LOOPBACK:
834 ctl |= TSIF_STS_CTL_EN_NULL |
835 TSIF_STS_CTL_EN_ERROR |
836 TSIF_STS_CTL_TEST_MODE;
837 break;
838 case TSPP_TSIF_MODE_1:
839 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
840 TSIF_STS_CTL_EN_TCR;
841 break;
842 case TSPP_TSIF_MODE_2:
843 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
844 TSIF_STS_CTL_EN_TCR |
845 TSIF_STS_CTL_MODE_2;
846 break;
847 default:
848 pr_warn("tspp: unknown tsif mode 0x%x",
849 tsif_device->mode);
Joel Nider5556a852011-10-16 10:52:13 +0200850 }
851 writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
852 writel_relaxed(tsif_device->time_limit,
853 tsif_device->base + TSIF_TIME_LIMIT_OFF);
854 wmb();
855 writel_relaxed(ctl | TSIF_STS_CTL_START,
856 tsif_device->base + TSIF_STS_CTL_OFF);
857 wmb();
Joel Nider5556a852011-10-16 10:52:13 +0200858 }
859
Joel Nider5bd73f82011-12-14 16:53:30 +0200860 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
Joel Nider5556a852011-10-16 10:52:13 +0200861 tsif_device->ref_count++;
862
Joel Nider5bd73f82011-12-14 16:53:30 +0200863 return (ctl & TSIF_STS_CTL_START) ? 0 : -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200864}
865
866static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
867{
868 if (tsif_device->ref_count == 0)
869 return;
870
871 tsif_device->ref_count--;
872
873 if (tsif_device->ref_count == 0) {
874 writel_relaxed(TSIF_STS_CTL_STOP,
875 tsif_device->base + TSIF_STS_CTL_OFF);
876 wmb();
877 }
878}
879
Joel Nider5bd73f82011-12-14 16:53:30 +0200880/*** local TSPP functions ***/
881static int tspp_channels_in_use(struct tspp_device *pdev)
882{
883 int i;
884 int count = 0;
885 for (i = 0; i < TSPP_NUM_CHANNELS; i++)
886 count += (pdev->channels[i].used ? 1 : 0);
887
888 return count;
889}
890
891static struct tspp_device *tspp_find_by_id(int id)
892{
893 struct tspp_device *dev;
894 list_for_each_entry(dev, &tspp_devices, devlist) {
895 if (dev->pdev->id == id)
896 return dev;
897 }
898 return NULL;
899}
900
Joel Nider5556a852011-10-16 10:52:13 +0200901static int tspp_get_key_entry(void)
902{
903 int i;
904 for (i = 0; i < TSPP_NUM_KEYS; i++) {
905 if (!(tspp_key_entry & (1 << i))) {
906 tspp_key_entry |= (1 << i);
907 return i;
908 }
909 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200910 return 1 < TSPP_NUM_KEYS;
Joel Nider5556a852011-10-16 10:52:13 +0200911}
912
913static void tspp_free_key_entry(int entry)
914{
915 if (entry > TSPP_NUM_KEYS) {
916 pr_err("tspp_free_key_entry: index out of bounds");
917 return;
918 }
919
920 tspp_key_entry &= ~(1 << entry);
921}
922
Joel Nider5bd73f82011-12-14 16:53:30 +0200923static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200924 u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user)
Joel Nider5556a852011-10-16 10:52:13 +0200925{
Joel Nider5bd73f82011-12-14 16:53:30 +0200926 if (size < TSPP_MIN_BUFFER_SIZE ||
927 size > TSPP_MAX_BUFFER_SIZE) {
928 pr_err("tspp: bad buffer size %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200929 return -ENOMEM;
930 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200931
932 if (alloc) {
933 TSPP_DEBUG("tspp using alloc function");
934 desc->virt_base = alloc(channel_id, size,
935 &desc->phys_base, user);
936 } else {
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200937 if (!dma_pool)
938 desc->virt_base = dma_alloc_coherent(NULL, size,
939 &desc->phys_base, GFP_KERNEL);
940 else
941 desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL,
942 &desc->phys_base);
943
Liron Kuch229090d2012-10-30 17:47:50 +0200944 if (desc->virt_base == 0) {
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200945 pr_err("tspp: dma buffer allocation failed %i\n", size);
Liron Kuch229090d2012-10-30 17:47:50 +0200946 return -ENOMEM;
947 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200948 }
949
950 desc->size = size;
951 return 0;
952}
953
954static int tspp_queue_buffer(struct tspp_channel *channel,
955 struct tspp_mem_buffer *buffer)
956{
957 int rc;
958 u32 flags = 0;
959
960 /* make sure the interrupt frequency is valid */
961 if (channel->int_freq < 1)
962 channel->int_freq = 1;
963
964 /* generate interrupt according to requested frequency */
965 if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
Hamad Kadmany567bed82012-11-29 14:15:57 +0200966 flags = SPS_IOVEC_FLAG_INT;
Joel Nider5bd73f82011-12-14 16:53:30 +0200967
968 /* start the transfer */
969 rc = sps_transfer_one(channel->pipe,
970 buffer->sps.phys_base,
971 buffer->sps.size,
972 channel->pdev,
973 flags);
974 if (rc < 0)
975 return rc;
976
977 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5556a852011-10-16 10:52:13 +0200978
979 return 0;
980}
981
982static int tspp_global_reset(struct tspp_device *pdev)
983{
984 u32 i, val;
985
986 /* stop all TSIFs */
987 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
988 pdev->tsif[i].ref_count = 1; /* allows stopping hw */
989 tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
990 pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200991 pdev->tsif[i].clock_inverse = 0;
992 pdev->tsif[i].data_inverse = 0;
993 pdev->tsif[i].sync_inverse = 0;
994 pdev->tsif[i].enable_inverse = 0;
Joel Nider5556a852011-10-16 10:52:13 +0200995 }
996 writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
997 wmb();
998
999 /* BAM */
1000 if (sps_device_reset(pdev->bam_handle) != 0) {
1001 pr_err("tspp: error resetting bam");
Joel Nider5bd73f82011-12-14 16:53:30 +02001002 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001003 }
1004
1005 /* TSPP tables */
1006 for (i = 0; i < TSPP_FILTER_TABLES; i++)
Joel Nider5bd73f82011-12-14 16:53:30 +02001007 memset(pdev->filters[i],
Joel Nider5556a852011-10-16 10:52:13 +02001008 0, sizeof(struct tspp_pid_filter_table));
1009
1010 /* disable all filters */
1011 val = (2 << TSPP_NUM_CHANNELS) - 1;
1012 writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
1013
1014 /* TSPP registers */
1015 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1016 writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1017 pdev->base + TSPP_CONTROL);
1018 wmb();
Joel Nider5bd73f82011-12-14 16:53:30 +02001019 memset(pdev->tspp_global_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001020 sizeof(struct tspp_global_performance_regs));
Joel Nider5bd73f82011-12-14 16:53:30 +02001021 memset(pdev->tspp_pipe_context, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001022 sizeof(struct tspp_pipe_context_regs));
Joel Nider5bd73f82011-12-14 16:53:30 +02001023 memset(pdev->tspp_pipe_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001024 sizeof(struct tspp_pipe_performance_regs));
1025 wmb();
1026 writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1027 pdev->base + TSPP_CONTROL);
1028 wmb();
1029
1030 val = readl_relaxed(pdev->base + TSPP_CONFIG);
1031 val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
1032 TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
1033 TSPP_CONFIG_PS_CONT_ERR_MASK);
1034 TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
1035 writel_relaxed(val, pdev->base + TSPP_CONFIG);
Hamad Kadmany57f5ac82012-12-20 18:30:40 +02001036 writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK);
Joel Nider5556a852011-10-16 10:52:13 +02001037 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
1038 writel_relaxed(0, pdev->base + TSPP_RST);
1039 wmb();
1040
1041 tspp_key_entry = 0;
1042
1043 return 0;
1044}
1045
Joel Nider5bd73f82011-12-14 16:53:30 +02001046static int tspp_select_source(u32 dev, u32 channel_id,
1047 struct tspp_select_source *src)
1048{
1049 /* make sure the requested src id is in bounds */
1050 if (src->source > TSPP_SOURCE_MEM) {
1051 pr_err("tspp source out of bounds");
1052 return -EINVAL;
1053 }
1054
1055 /* open the stream */
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001056 tspp_open_stream(dev, channel_id, src);
Joel Nider5bd73f82011-12-14 16:53:30 +02001057
1058 return 0;
1059}
1060
1061static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
1062{
1063 struct tspp_device *pdev = channel->pdev;
1064
1065 writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
1066 writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
1067 return 0;
1068}
1069
1070static int tspp_set_system_keys(struct tspp_channel *channel,
1071 struct tspp_system_keys *keys)
1072{
1073 int i;
1074 struct tspp_device *pdev = channel->pdev;
1075
1076 for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
1077 writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
1078
1079 return 0;
1080}
1081
1082static int tspp_channel_init(struct tspp_channel *channel,
1083 struct tspp_device *pdev)
1084{
1085 channel->cdev.owner = THIS_MODULE;
1086 cdev_init(&channel->cdev, &tspp_fops);
1087 channel->pdev = pdev;
1088 channel->data = NULL;
1089 channel->read = NULL;
1090 channel->waiting = NULL;
1091 channel->locked = NULL;
1092 channel->id = MINOR(tspp_minor);
1093 channel->used = 0;
1094 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1095 channel->max_buffers = TSPP_NUM_BUFFERS;
1096 channel->buffer_count = 0;
1097 channel->filter_count = 0;
1098 channel->int_freq = 1;
Liron Kuch229090d2012-10-30 17:47:50 +02001099 channel->src = TSPP_SOURCE_NONE;
1100 channel->mode = TSPP_MODE_DISABLED;
Joel Nider5bd73f82011-12-14 16:53:30 +02001101 channel->notifier = NULL;
1102 channel->notify_data = NULL;
Hamad Kadmany567bed82012-11-29 14:15:57 +02001103 channel->expiration_period_ms = 0;
Liron Kuch229090d2012-10-30 17:47:50 +02001104 channel->memfree = NULL;
1105 channel->user_info = NULL;
Joel Nider5bd73f82011-12-14 16:53:30 +02001106 init_waitqueue_head(&channel->in_queue);
1107
1108 if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
1109 pr_err("tspp: cdev_add failed");
1110 return -EBUSY;
1111 }
1112
1113 channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
1114 channel, "tspp%02d", channel->id);
1115 if (IS_ERR(channel->dd)) {
1116 pr_err("tspp: device_create failed: %i",
1117 (int)PTR_ERR(channel->dd));
1118 cdev_del(&channel->cdev);
1119 return -EBUSY;
1120 }
1121
1122 return 0;
1123}
1124
1125static int tspp_set_buffer_size(struct tspp_channel *channel,
1126 struct tspp_buffer *buf)
1127{
Liron Kuch229090d2012-10-30 17:47:50 +02001128 if (channel->buffer_count > 0) {
1129 pr_err("tspp: cannot set buffer size - buffers already allocated\n");
1130 return -EPERM;
1131 }
1132
Joel Nider5bd73f82011-12-14 16:53:30 +02001133 if (buf->size < TSPP_MIN_BUFFER_SIZE)
1134 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1135 else if (buf->size > TSPP_MAX_BUFFER_SIZE)
1136 channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
1137 else
1138 channel->buffer_size = buf->size;
1139
1140 return 0;
1141}
1142
1143static void tspp_set_tsif_mode(struct tspp_channel *channel,
1144 enum tspp_tsif_mode mode)
1145{
1146 int index;
1147
1148 switch (channel->src) {
1149 case TSPP_SOURCE_TSIF0:
1150 index = 0;
1151 break;
1152 case TSPP_SOURCE_TSIF1:
1153 index = 1;
1154 break;
1155 default:
1156 pr_warn("tspp: can't set mode for non-tsif source %d",
1157 channel->src);
1158 return;
1159 }
1160 channel->pdev->tsif[index].mode = mode;
1161}
1162
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001163static void tspp_set_signal_inversion(struct tspp_channel *channel,
Liron Kuch229090d2012-10-30 17:47:50 +02001164 int clock_inverse, int data_inverse,
1165 int sync_inverse, int enable_inverse)
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001166{
1167 int index;
1168
1169 switch (channel->src) {
1170 case TSPP_SOURCE_TSIF0:
1171 index = 0;
1172 break;
1173 case TSPP_SOURCE_TSIF1:
1174 index = 1;
1175 break;
1176 default:
1177 return;
1178 }
1179 channel->pdev->tsif[index].clock_inverse = clock_inverse;
1180 channel->pdev->tsif[index].data_inverse = data_inverse;
1181 channel->pdev->tsif[index].sync_inverse = sync_inverse;
1182 channel->pdev->tsif[index].enable_inverse = enable_inverse;
1183}
1184
Liron Kuch229090d2012-10-30 17:47:50 +02001185static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode)
1186{
1187 u32 alignment;
1188
1189 switch (mode) {
1190 case TSPP_MODE_RAW:
1191 /* must be a multiple of 192 */
1192 alignment = (TSPP_PACKET_LENGTH + 4);
1193 if (size % alignment)
1194 return 0;
1195 return 1;
1196
1197 case TSPP_MODE_RAW_NO_SUFFIX:
1198 /* must be a multiple of 188 */
1199 alignment = TSPP_PACKET_LENGTH;
1200 if (size % alignment)
1201 return 0;
1202 return 1;
1203
1204 case TSPP_MODE_DISABLED:
1205 case TSPP_MODE_PES:
1206 default:
1207 /* no alignment requirement */
1208 return 1;
1209 }
1210
1211}
1212
1213static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode)
1214{
1215 u32 new_size;
1216 u32 alignment;
1217
1218 switch (mode) {
1219 case TSPP_MODE_RAW:
1220 /* must be a multiple of 192 */
1221 alignment = (TSPP_PACKET_LENGTH + 4);
1222 break;
1223
1224 case TSPP_MODE_RAW_NO_SUFFIX:
1225 /* must be a multiple of 188 */
1226 alignment = TSPP_PACKET_LENGTH;
1227 break;
1228
1229 case TSPP_MODE_DISABLED:
1230 case TSPP_MODE_PES:
1231 default:
1232 /* no alignment requirement - give the user what he asks for */
1233 alignment = 1;
1234 break;
1235 }
1236 /* align up */
1237 new_size = (((size + alignment - 1) / alignment) * alignment);
1238 return new_size;
1239}
1240
1241static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel)
1242{
1243 int i;
1244 struct tspp_mem_buffer *pbuf, *temp;
1245
1246 pbuf = channel->data;
1247 for (i = 0; i < channel->buffer_count; i++) {
1248 if (pbuf->desc.phys_base) {
1249 if (channel->memfree) {
1250 channel->memfree(channel_id,
1251 pbuf->desc.size,
1252 pbuf->desc.virt_base,
1253 pbuf->desc.phys_base,
1254 channel->user_info);
1255 } else {
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001256 if (!channel->dma_pool)
1257 dma_free_coherent(NULL,
1258 pbuf->desc.size,
1259 pbuf->desc.virt_base,
1260 pbuf->desc.phys_base);
1261 else
1262 dma_pool_free(channel->dma_pool,
1263 pbuf->desc.virt_base,
1264 pbuf->desc.phys_base);
Liron Kuch229090d2012-10-30 17:47:50 +02001265 }
1266 pbuf->desc.phys_base = 0;
1267 }
1268 pbuf->desc.virt_base = 0;
1269 pbuf->state = TSPP_BUF_STATE_EMPTY;
1270 temp = pbuf;
1271 pbuf = pbuf->next;
1272 kfree(temp);
1273 }
1274}
1275
Joel Nider5bd73f82011-12-14 16:53:30 +02001276/*** TSPP API functions ***/
Liron Kuch229090d2012-10-30 17:47:50 +02001277
1278/**
1279 * tspp_open_stream - open a TSPP stream for use.
1280 *
1281 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1282 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1283 * @source: stream source parameters.
1284 *
1285 * Return error status
1286 *
1287 */
1288int tspp_open_stream(u32 dev, u32 channel_id,
1289 struct tspp_select_source *source)
Joel Nider5556a852011-10-16 10:52:13 +02001290{
1291 u32 val;
1292 struct tspp_device *pdev;
Joel Nider5bd73f82011-12-14 16:53:30 +02001293 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001294
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001295 TSPP_DEBUG("tspp_open_stream %i %i %i %i",
1296 dev, channel_id, source->source, source->mode);
Liron Kuch229090d2012-10-30 17:47:50 +02001297
Joel Nider5bd73f82011-12-14 16:53:30 +02001298 if (dev >= TSPP_MAX_DEVICES) {
1299 pr_err("tspp: device id out of range");
1300 return -ENODEV;
1301 }
Joel Nider5556a852011-10-16 10:52:13 +02001302
Joel Nider5bd73f82011-12-14 16:53:30 +02001303 if (channel_id >= TSPP_NUM_CHANNELS) {
1304 pr_err("tspp: channel id out of range");
1305 return -ECHRNG;
1306 }
1307
1308 pdev = tspp_find_by_id(dev);
1309 if (!pdev) {
1310 pr_err("tspp_str: can't find device %i", dev);
1311 return -ENODEV;
1312 }
1313 channel = &pdev->channels[channel_id];
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001314 channel->src = source->source;
1315 tspp_set_tsif_mode(channel, source->mode);
1316 tspp_set_signal_inversion(channel, source->clk_inverse,
Liron Kuch229090d2012-10-30 17:47:50 +02001317 source->data_inverse, source->sync_inverse,
1318 source->enable_inverse);
Joel Nider5556a852011-10-16 10:52:13 +02001319
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001320 switch (source->source) {
Joel Nider5556a852011-10-16 10:52:13 +02001321 case TSPP_SOURCE_TSIF0:
Liron Kuch65067fa2013-02-10 15:19:32 +02001322 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1323 pr_err("tspp: error enabling tsif0 GPIOs\n");
1324 return -EBUSY;
1325 }
Joel Nider5556a852011-10-16 10:52:13 +02001326 /* make sure TSIF0 is running & enabled */
1327 if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
1328 pr_err("tspp: error starting tsif0");
Joel Nider5bd73f82011-12-14 16:53:30 +02001329 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001330 }
Liron Kuchc2392df2013-02-14 16:26:38 +02001331 if (pdev->tsif[0].ref_count == 1) {
1332 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1333 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1334 pdev->base + TSPP_CONTROL);
1335 wmb();
1336 }
Joel Nider5556a852011-10-16 10:52:13 +02001337 break;
1338 case TSPP_SOURCE_TSIF1:
Liron Kuch65067fa2013-02-10 15:19:32 +02001339 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1340 pr_err("tspp: error enabling tsif1 GPIOs\n");
1341 return -EBUSY;
1342 }
Joel Nider5556a852011-10-16 10:52:13 +02001343 /* make sure TSIF1 is running & enabled */
1344 if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
1345 pr_err("tspp: error starting tsif1");
Joel Nider5bd73f82011-12-14 16:53:30 +02001346 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001347 }
Liron Kuchc2392df2013-02-14 16:26:38 +02001348 if (pdev->tsif[1].ref_count == 1) {
1349 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1350 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1351 pdev->base + TSPP_CONTROL);
1352 wmb();
1353 }
Joel Nider5556a852011-10-16 10:52:13 +02001354 break;
1355 case TSPP_SOURCE_MEM:
1356 break;
1357 default:
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001358 pr_err("tspp: channel %i invalid source %i",
1359 channel->id, source->source);
Joel Nider5bd73f82011-12-14 16:53:30 +02001360 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001361 }
1362
Joel Nider5556a852011-10-16 10:52:13 +02001363 return 0;
1364}
1365EXPORT_SYMBOL(tspp_open_stream);
1366
Liron Kuch229090d2012-10-30 17:47:50 +02001367/**
1368 * tspp_close_stream - close a TSPP stream.
1369 *
1370 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1371 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1372 *
1373 * Return error status
1374 *
1375 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001376int tspp_close_stream(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001377{
1378 u32 val;
Liron Kuchc2392df2013-02-14 16:26:38 +02001379 u32 prev_ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001380 struct tspp_device *pdev;
Joel Nider5bd73f82011-12-14 16:53:30 +02001381 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001382
Joel Nider5bd73f82011-12-14 16:53:30 +02001383 if (channel_id >= TSPP_NUM_CHANNELS) {
1384 pr_err("tspp: channel id out of range");
1385 return -ECHRNG;
1386 }
1387 pdev = tspp_find_by_id(dev);
1388 if (!pdev) {
1389 pr_err("tspp_cs: can't find device %i", dev);
1390 return -EBUSY;
1391 }
1392 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001393
1394 switch (channel->src) {
1395 case TSPP_SOURCE_TSIF0:
Liron Kuchc2392df2013-02-14 16:26:38 +02001396 prev_ref_count = pdev->tsif[0].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001397 tspp_stop_tsif(&pdev->tsif[0]);
Liron Kuch65067fa2013-02-10 15:19:32 +02001398 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1399 pr_err("tspp: error disabling tsif0 GPIOs\n");
1400
Liron Kuchc2392df2013-02-14 16:26:38 +02001401 if (prev_ref_count == 1) {
1402 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1403 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1404 pdev->base + TSPP_CONTROL);
1405 wmb();
1406 }
Joel Nider5556a852011-10-16 10:52:13 +02001407 break;
1408 case TSPP_SOURCE_TSIF1:
Liron Kuchc2392df2013-02-14 16:26:38 +02001409 prev_ref_count = pdev->tsif[1].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001410 tspp_stop_tsif(&pdev->tsif[1]);
Liron Kuch65067fa2013-02-10 15:19:32 +02001411 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1412 pr_err("tspp: error disabling tsif0 GPIOs\n");
1413
Liron Kuchc2392df2013-02-14 16:26:38 +02001414 if (prev_ref_count == 1) {
1415 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1416 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1417 pdev->base + TSPP_CONTROL);
1418 wmb();
1419 }
Joel Nider5556a852011-10-16 10:52:13 +02001420 break;
1421 case TSPP_SOURCE_MEM:
1422 break;
1423 case TSPP_SOURCE_NONE:
1424 break;
1425 }
1426
Joel Nider5bd73f82011-12-14 16:53:30 +02001427 channel->src = TSPP_SOURCE_NONE;
Joel Nider5556a852011-10-16 10:52:13 +02001428 return 0;
1429}
1430EXPORT_SYMBOL(tspp_close_stream);
1431
Liron Kuch229090d2012-10-30 17:47:50 +02001432/**
1433 * tspp_open_channel - open a TSPP channel.
1434 *
1435 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1436 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1437 *
1438 * Return error status
1439 *
1440 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001441int tspp_open_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001442{
1443 int rc = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001444 struct sps_connect *config;
1445 struct sps_register_event *event;
1446 struct tspp_channel *channel;
1447 struct tspp_device *pdev;
1448
1449 if (channel_id >= TSPP_NUM_CHANNELS) {
1450 pr_err("tspp: channel id out of range");
1451 return -ECHRNG;
1452 }
1453 pdev = tspp_find_by_id(dev);
1454 if (!pdev) {
1455 pr_err("tspp_oc: can't find device %i", dev);
1456 return -ENODEV;
1457 }
1458 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001459
1460 if (channel->used) {
1461 pr_err("tspp channel already in use");
Joel Nider5bd73f82011-12-14 16:53:30 +02001462 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001463 }
1464
Joel Nider5bd73f82011-12-14 16:53:30 +02001465 config = &channel->config;
1466 event = &channel->event;
1467
1468 /* start the clocks if needed */
Liron Kuch8fa85b02013-01-01 18:29:47 +02001469 if (tspp_channels_in_use(pdev) == 0) {
Liron Kuchde8cbf92013-02-21 14:25:57 +02001470 rc = tspp_clock_start(pdev);
1471 if (rc)
1472 return rc;
1473
Joel Nider5bd73f82011-12-14 16:53:30 +02001474 wake_lock(&pdev->wake_lock);
Liron Kuch8fa85b02013-01-01 18:29:47 +02001475 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001476
Joel Nider5556a852011-10-16 10:52:13 +02001477 /* mark it as used */
1478 channel->used = 1;
1479
1480 /* start the bam */
1481 channel->pipe = sps_alloc_endpoint();
1482 if (channel->pipe == 0) {
1483 pr_err("tspp: error allocating endpoint");
1484 rc = -ENOMEM;
1485 goto err_sps_alloc;
1486 }
1487
1488 /* get default configuration */
1489 sps_get_config(channel->pipe, config);
1490
Joel Nider5bd73f82011-12-14 16:53:30 +02001491 config->source = pdev->bam_handle;
Joel Nider5556a852011-10-16 10:52:13 +02001492 config->destination = SPS_DEV_HANDLE_MEM;
1493 config->mode = SPS_MODE_SRC;
Joel Nider5bd73f82011-12-14 16:53:30 +02001494 config->options =
1495 SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
1496 SPS_O_STREAMING | /* streaming mode */
1497 SPS_O_DESC_DONE | /* interrupt on end of descriptor */
Hamad Kadmany567bed82012-11-29 14:15:57 +02001498 SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */
1499 SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */
Joel Nider5556a852011-10-16 10:52:13 +02001500 config->src_pipe_index = channel->id;
1501 config->desc.size =
Hamad Kadmany567bed82012-11-29 14:15:57 +02001502 TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE;
Joel Nider5556a852011-10-16 10:52:13 +02001503 config->desc.base = dma_alloc_coherent(NULL,
1504 config->desc.size,
1505 &config->desc.phys_base,
1506 GFP_KERNEL);
1507 if (config->desc.base == 0) {
1508 pr_err("tspp: error allocating sps descriptors");
1509 rc = -ENOMEM;
1510 goto err_desc_alloc;
1511 }
1512
1513 memset(config->desc.base, 0, config->desc.size);
1514
1515 rc = sps_connect(channel->pipe, config);
1516 if (rc) {
1517 pr_err("tspp: error connecting bam");
1518 goto err_connect;
1519 }
1520
1521 event->mode = SPS_TRIGGER_CALLBACK;
Joel Nider5bd73f82011-12-14 16:53:30 +02001522 event->options = SPS_O_DESC_DONE;
Joel Nider5556a852011-10-16 10:52:13 +02001523 event->callback = tspp_sps_complete_cb;
1524 event->xfer_done = NULL;
Joel Nider5bd73f82011-12-14 16:53:30 +02001525 event->user = pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001526
1527 rc = sps_register_event(channel->pipe, event);
1528 if (rc) {
1529 pr_err("tspp: error registering event");
1530 goto err_event;
1531 }
1532
Hamad Kadmany567bed82012-11-29 14:15:57 +02001533 init_timer(&channel->expiration_timer);
1534 channel->expiration_timer.function = tspp_expiration_timer;
1535 channel->expiration_timer.data = (unsigned long)pdev;
1536 channel->expiration_timer.expires = 0xffffffffL;
1537
Joel Nider5bd73f82011-12-14 16:53:30 +02001538 rc = pm_runtime_get(&pdev->pdev->dev);
Joel Nider5556a852011-10-16 10:52:13 +02001539 if (rc < 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001540 dev_err(&pdev->pdev->dev,
Joel Nider5556a852011-10-16 10:52:13 +02001541 "Runtime PM: Unable to wake up tspp device, rc = %d",
1542 rc);
1543 }
Joel Nider5556a852011-10-16 10:52:13 +02001544 return 0;
1545
1546err_event:
1547 sps_disconnect(channel->pipe);
1548err_connect:
1549 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1550 config->desc.phys_base);
1551err_desc_alloc:
1552 sps_free_endpoint(channel->pipe);
1553err_sps_alloc:
1554 return rc;
1555}
1556EXPORT_SYMBOL(tspp_open_channel);
1557
Liron Kuch229090d2012-10-30 17:47:50 +02001558/**
1559 * tspp_close_channel - close a TSPP channel.
1560 *
1561 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1562 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1563 *
1564 * Return error status
1565 *
1566 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001567int tspp_close_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001568{
1569 int i;
1570 int id;
Liron Kuch98fb17c2013-03-28 09:39:17 +02001571 int table_idx;
Joel Nider5556a852011-10-16 10:52:13 +02001572 u32 val;
Liron Kuchf2fa7102013-04-21 13:16:45 +03001573 unsigned long flags;
Joel Nider5556a852011-10-16 10:52:13 +02001574
Joel Nider5bd73f82011-12-14 16:53:30 +02001575 struct sps_connect *config;
1576 struct tspp_device *pdev;
1577 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02001578
1579 if (channel_id >= TSPP_NUM_CHANNELS) {
1580 pr_err("tspp: channel id out of range");
1581 return -ECHRNG;
1582 }
1583 pdev = tspp_find_by_id(dev);
1584 if (!pdev) {
1585 pr_err("tspp_close: can't find device %i", dev);
1586 return -ENODEV;
1587 }
1588 channel = &pdev->channels[channel_id];
1589
1590 /* if the channel is not used, we are done */
1591 if (!channel->used)
1592 return 0;
1593
Liron Kuchf2fa7102013-04-21 13:16:45 +03001594 /*
1595 * Need to protect access to used and waiting fields, as they are
1596 * used by the tasklet which is invoked from interrupt context
1597 */
1598 spin_lock_irqsave(&pdev->spinlock, flags);
1599 channel->used = 0;
1600 channel->waiting = NULL;
1601 spin_unlock_irqrestore(&pdev->spinlock, flags);
1602
Hamad Kadmany567bed82012-11-29 14:15:57 +02001603 if (channel->expiration_period_ms)
1604 del_timer(&channel->expiration_timer);
1605
Joel Nider5bd73f82011-12-14 16:53:30 +02001606 channel->notifier = NULL;
1607 channel->notify_data = NULL;
Hamad Kadmany567bed82012-11-29 14:15:57 +02001608 channel->expiration_period_ms = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001609
1610 config = &channel->config;
1611 pdev = channel->pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001612
1613 /* disable pipe (channel) */
1614 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1615 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1616 wmb();
1617
1618 /* unregister all filters for this channel */
Liron Kuch98fb17c2013-03-28 09:39:17 +02001619 for (table_idx = 0; table_idx < TSPP_FILTER_TABLES; table_idx++) {
1620 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1621 struct tspp_pid_filter *filter =
1622 &pdev->filters[table_idx]->filter[i];
1623 id = FILTER_GET_PIPE_NUMBER0(filter);
1624 if (id == channel->id) {
1625 if (FILTER_HAS_ENCRYPTION(filter))
1626 tspp_free_key_entry(
1627 FILTER_GET_KEY_NUMBER(filter));
1628 filter->config = 0;
1629 filter->filter = 0;
1630 }
Joel Nider5556a852011-10-16 10:52:13 +02001631 }
1632 }
1633 channel->filter_count = 0;
1634
Joel Nider5556a852011-10-16 10:52:13 +02001635 /* disconnect the bam */
1636 if (sps_disconnect(channel->pipe) != 0)
1637 pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
1638
1639 /* destroy the buffers */
1640 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1641 config->desc.phys_base);
1642
Liron Kuch229090d2012-10-30 17:47:50 +02001643 tspp_destroy_buffers(channel_id, channel);
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001644 if (channel->dma_pool) {
1645 dma_pool_destroy(channel->dma_pool);
1646 channel->dma_pool = NULL;
1647 }
Liron Kuch229090d2012-10-30 17:47:50 +02001648
1649 channel->src = TSPP_SOURCE_NONE;
1650 channel->mode = TSPP_MODE_DISABLED;
1651 channel->memfree = NULL;
1652 channel->user_info = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001653 channel->buffer_count = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001654 channel->data = NULL;
1655 channel->read = NULL;
Joel Nider5bd73f82011-12-14 16:53:30 +02001656 channel->locked = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001657
Liron Kuch8fa85b02013-01-01 18:29:47 +02001658 if (tspp_channels_in_use(pdev) == 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001659 wake_unlock(&pdev->wake_lock);
Liron Kuch8fa85b02013-01-01 18:29:47 +02001660 tspp_clock_stop(pdev);
1661 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001662
Liron Kuchde8cbf92013-02-21 14:25:57 +02001663 pm_runtime_put(&pdev->pdev->dev);
1664
Joel Nider5556a852011-10-16 10:52:13 +02001665 return 0;
1666}
1667EXPORT_SYMBOL(tspp_close_channel);
1668
Liron Kuch229090d2012-10-30 17:47:50 +02001669/**
Hamad Kadmany586fb392013-01-31 14:49:20 +02001670 * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter.
1671 *
1672 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1673 * @source: The TSIF source from which the counter should be read
1674 * @tcr_counter: the value of TCR counter
1675 *
1676 * Return error status
1677 *
1678 * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz.
1679 * If source is neither TSIF 0 or TSIF1 0 is returned.
1680 */
1681int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter)
1682{
1683 struct tspp_device *pdev;
1684 struct tspp_tsif_device *tsif_device;
1685
1686 if (!tcr_counter)
1687 return -EINVAL;
1688
1689 pdev = tspp_find_by_id(dev);
1690 if (!pdev) {
1691 pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev);
1692 return -ENODEV;
1693 }
1694
1695 switch (source) {
1696 case TSPP_SOURCE_TSIF0:
1697 tsif_device = &pdev->tsif[0];
1698 break;
1699
1700 case TSPP_SOURCE_TSIF1:
1701 tsif_device = &pdev->tsif[1];
1702 break;
1703
1704 default:
1705 tsif_device = NULL;
1706 break;
1707 }
1708
1709 if (tsif_device && tsif_device->ref_count)
1710 *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF);
1711 else
1712 *tcr_counter = 0;
1713
1714 return 0;
1715}
1716EXPORT_SYMBOL(tspp_get_ref_clk_counter);
1717
1718/**
Liron Kuch229090d2012-10-30 17:47:50 +02001719 * tspp_add_filter - add a TSPP filter to a channel.
1720 *
1721 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1722 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1723 * @filter: TSPP filter parameters
1724 *
1725 * Return error status
1726 *
1727 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001728int tspp_add_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001729 struct tspp_filter *filter)
1730{
Liron Kuch229090d2012-10-30 17:47:50 +02001731 int i, rc;
Joel Nider5556a852011-10-16 10:52:13 +02001732 int other_channel;
1733 int entry;
1734 u32 val, pid, enabled;
Joel Nider5bd73f82011-12-14 16:53:30 +02001735 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001736 struct tspp_pid_filter p;
Joel Nider5bd73f82011-12-14 16:53:30 +02001737 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001738
Joel Nider5bd73f82011-12-14 16:53:30 +02001739 TSPP_DEBUG("tspp: add filter");
1740 if (channel_id >= TSPP_NUM_CHANNELS) {
1741 pr_err("tspp: channel id out of range");
1742 return -ECHRNG;
1743 }
1744 pdev = tspp_find_by_id(dev);
1745 if (!pdev) {
1746 pr_err("tspp_add: can't find device %i", dev);
1747 return -ENODEV;
1748 }
1749
1750 channel = &pdev->channels[channel_id];
1751
Joel Nider5556a852011-10-16 10:52:13 +02001752 if (filter->source > TSPP_SOURCE_MEM) {
1753 pr_err("tspp invalid source");
Joel Nider5bd73f82011-12-14 16:53:30 +02001754 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001755 }
1756
1757 if (filter->priority >= TSPP_NUM_PRIORITIES) {
1758 pr_err("tspp invalid source");
Joel Nider5bd73f82011-12-14 16:53:30 +02001759 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001760 }
1761
Liron Kuch229090d2012-10-30 17:47:50 +02001762 channel->mode = filter->mode;
1763 /*
1764 * if buffers are already allocated, verify they fulfil
1765 * the alignment requirements.
1766 */
1767 if ((channel->buffer_count > 0) &&
1768 (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode)))
1769 pr_warn("tspp: buffers allocated with incorrect alignment\n");
Joel Nider5556a852011-10-16 10:52:13 +02001770
1771 if (filter->mode == TSPP_MODE_PES) {
1772 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1773 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001774 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001775 pid = FILTER_GET_PIPE_PID((tspp_filter));
1776 enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
1777 if (enabled && (pid == filter->pid)) {
1778 other_channel =
1779 FILTER_GET_PIPE_NUMBER0(tspp_filter);
1780 pr_err("tspp: pid 0x%x already in use by channel %i",
1781 filter->pid, other_channel);
Joel Nider5bd73f82011-12-14 16:53:30 +02001782 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001783 }
1784 }
1785 }
1786
1787 /* make sure this priority is not already in use */
1788 enabled = FILTER_GET_PIPE_PROCESS0(
Joel Nider5bd73f82011-12-14 16:53:30 +02001789 (&(pdev->filters[channel->src]->filter[filter->priority])));
Joel Nider5556a852011-10-16 10:52:13 +02001790 if (enabled) {
1791 pr_err("tspp: filter priority %i source %i is already enabled\n",
1792 filter->priority, channel->src);
Joel Nider5bd73f82011-12-14 16:53:30 +02001793 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001794 }
1795
1796 if (channel->mode == TSPP_MODE_PES) {
1797 /* if we are already processing in PES mode, disable pipe
1798 (channel) and filter to be updated */
1799 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1800 writel_relaxed(val | (1 << channel->id),
1801 pdev->base + TSPP_PS_DISABLE);
1802 wmb();
1803 }
1804
1805 /* update entry */
1806 p.filter = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001807 p.config = FILTER_TRANS_END_DISABLE;
Joel Nider5556a852011-10-16 10:52:13 +02001808 FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
1809 FILTER_SET_PIPE_PID((&p), filter->pid);
1810 FILTER_SET_PID_MASK((&p), filter->mask);
1811 FILTER_SET_PIPE_NUMBER0((&p), channel->id);
1812 FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
1813 if (filter->decrypt) {
1814 entry = tspp_get_key_entry();
1815 if (entry == -1) {
1816 pr_err("tspp: no more keys available!");
1817 } else {
1818 p.config |= FILTER_DECRYPT;
1819 FILTER_SET_KEY_NUMBER((&p), entry);
1820 }
1821 }
Joel Nider5556a852011-10-16 10:52:13 +02001822
Joel Nider5bd73f82011-12-14 16:53:30 +02001823 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001824 filter[filter->priority].config = p.config;
Joel Nider5bd73f82011-12-14 16:53:30 +02001825 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001826 filter[filter->priority].filter = p.filter;
1827
Liron Kuch229090d2012-10-30 17:47:50 +02001828 /*
1829 * allocate buffers if needed (i.e. if user did has not already called
1830 * tspp_allocate_buffers() explicitly).
1831 */
1832 if (channel->buffer_count == 0) {
1833 channel->buffer_size =
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001834 tspp_align_buffer_size_by_mode(channel->buffer_size,
Liron Kuch229090d2012-10-30 17:47:50 +02001835 channel->mode);
1836 rc = tspp_allocate_buffers(dev, channel->id,
1837 channel->max_buffers,
1838 channel->buffer_size,
1839 channel->int_freq, NULL, NULL, NULL);
1840 if (rc != 0) {
1841 pr_err("tspp: tspp_allocate_buffers failed\n");
1842 return rc;
1843 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001844 }
1845
Joel Nider5556a852011-10-16 10:52:13 +02001846 /* reenable pipe */
1847 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1848 writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
1849 wmb();
1850 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1851
Joel Nider5556a852011-10-16 10:52:13 +02001852 channel->filter_count++;
1853
1854 return 0;
1855}
1856EXPORT_SYMBOL(tspp_add_filter);
1857
Liron Kuch229090d2012-10-30 17:47:50 +02001858/**
1859 * tspp_remove_filter - remove a TSPP filter from a channel.
1860 *
1861 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1862 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1863 * @filter: TSPP filter parameters
1864 *
1865 * Return error status
1866 *
1867 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001868int tspp_remove_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001869 struct tspp_filter *filter)
1870{
1871 int entry;
1872 u32 val;
Joel Nider5bd73f82011-12-14 16:53:30 +02001873 struct tspp_device *pdev;
1874 int src;
1875 struct tspp_pid_filter *tspp_filter;
1876 struct tspp_channel *channel;
1877
1878 if (channel_id >= TSPP_NUM_CHANNELS) {
1879 pr_err("tspp: channel id out of range");
1880 return -ECHRNG;
1881 }
1882 pdev = tspp_find_by_id(dev);
1883 if (!pdev) {
1884 pr_err("tspp_remove: can't find device %i", dev);
1885 return -ENODEV;
1886 }
1887 channel = &pdev->channels[channel_id];
1888
1889 src = channel->src;
1890 tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
Joel Nider5556a852011-10-16 10:52:13 +02001891
1892 /* disable pipe (channel) */
1893 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1894 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1895 wmb();
1896
1897 /* update data keys */
1898 if (tspp_filter->config & FILTER_DECRYPT) {
1899 entry = FILTER_GET_KEY_NUMBER(tspp_filter);
1900 tspp_free_key_entry(entry);
1901 }
1902
1903 /* update pid table */
1904 tspp_filter->config = 0;
1905 tspp_filter->filter = 0;
1906
1907 channel->filter_count--;
1908
1909 /* reenable pipe */
1910 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1911 writel_relaxed(val & ~(1 << channel->id),
1912 pdev->base + TSPP_PS_DISABLE);
1913 wmb();
1914 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1915
1916 return 0;
1917}
1918EXPORT_SYMBOL(tspp_remove_filter);
1919
Liron Kuch229090d2012-10-30 17:47:50 +02001920/**
1921 * tspp_set_key - set TSPP key in key table.
1922 *
1923 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1924 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1925 * @key: TSPP key parameters
1926 *
1927 * Return error status
1928 *
1929 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001930int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
Joel Nider5556a852011-10-16 10:52:13 +02001931{
1932 int i;
1933 int id;
1934 int key_index;
1935 int data;
Joel Nider5bd73f82011-12-14 16:53:30 +02001936 struct tspp_channel *channel;
1937 struct tspp_device *pdev;
1938
1939 if (channel_id >= TSPP_NUM_CHANNELS) {
1940 pr_err("tspp: channel id out of range");
1941 return -ECHRNG;
1942 }
1943 pdev = tspp_find_by_id(dev);
1944 if (!pdev) {
1945 pr_err("tspp_set: can't find device %i", dev);
1946 return -ENODEV;
1947 }
1948 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001949
1950 /* read the key index used by this channel */
1951 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1952 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001953 &(pdev->filters[channel->src]->filter[i]);
Joel Nider5556a852011-10-16 10:52:13 +02001954 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1955 if (id == channel->id) {
1956 if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
1957 key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
1958 break;
1959 }
1960 }
1961 }
1962 if (i == TSPP_NUM_PRIORITIES) {
1963 pr_err("tspp: no encryption on this channel");
Joel Nider5bd73f82011-12-14 16:53:30 +02001964 return -ENOKEY;
Joel Nider5556a852011-10-16 10:52:13 +02001965 }
1966
1967 if (key->parity == TSPP_KEY_PARITY_EVEN) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001968 pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
1969 pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001970 } else {
Joel Nider5bd73f82011-12-14 16:53:30 +02001971 pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
1972 pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001973 }
1974 data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
1975
1976 return 0;
1977}
1978EXPORT_SYMBOL(tspp_set_key);
1979
Liron Kuch229090d2012-10-30 17:47:50 +02001980/**
1981 * tspp_register_notification - register TSPP channel notification function.
1982 *
1983 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1984 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1985 * @pNotify: notification function
1986 * @userdata: user data to pass to notification function
1987 * @timer_ms: notification for partially filled buffers
1988 *
1989 * Return error status
1990 *
1991 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001992int tspp_register_notification(u32 dev, u32 channel_id,
1993 tspp_notifier *pNotify, void *userdata, u32 timer_ms)
Joel Nider5556a852011-10-16 10:52:13 +02001994{
Joel Nider5bd73f82011-12-14 16:53:30 +02001995 struct tspp_channel *channel;
1996 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001997
Joel Nider5bd73f82011-12-14 16:53:30 +02001998 if (channel_id >= TSPP_NUM_CHANNELS) {
1999 pr_err("tspp: channel id out of range");
2000 return -ECHRNG;
2001 }
2002 pdev = tspp_find_by_id(dev);
2003 if (!pdev) {
2004 pr_err("tspp_reg: can't find device %i", dev);
2005 return -ENODEV;
2006 }
2007 channel = &pdev->channels[channel_id];
2008 channel->notifier = pNotify;
2009 channel->notify_data = userdata;
Hamad Kadmany567bed82012-11-29 14:15:57 +02002010 channel->expiration_period_ms = timer_ms;
2011
Joel Nider5556a852011-10-16 10:52:13 +02002012 return 0;
2013}
Joel Nider5bd73f82011-12-14 16:53:30 +02002014EXPORT_SYMBOL(tspp_register_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002015
Liron Kuch229090d2012-10-30 17:47:50 +02002016/**
2017 * tspp_unregister_notification - unregister TSPP channel notification function.
2018 *
2019 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2020 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2021 *
2022 * Return error status
2023 *
2024 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002025int tspp_unregister_notification(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002026{
Joel Nider5bd73f82011-12-14 16:53:30 +02002027 struct tspp_channel *channel;
2028 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002029
Joel Nider5bd73f82011-12-14 16:53:30 +02002030 if (channel_id >= TSPP_NUM_CHANNELS) {
2031 pr_err("tspp: channel id out of range");
2032 return -ECHRNG;
2033 }
2034 pdev = tspp_find_by_id(dev);
2035 if (!pdev) {
2036 pr_err("tspp_unreg: can't find device %i", dev);
2037 return -ENODEV;
2038 }
2039 channel = &pdev->channels[channel_id];
2040 channel->notifier = NULL;
2041 channel->notify_data = 0;
Joel Nider5556a852011-10-16 10:52:13 +02002042 return 0;
2043}
Joel Nider5bd73f82011-12-14 16:53:30 +02002044EXPORT_SYMBOL(tspp_unregister_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002045
Liron Kuch229090d2012-10-30 17:47:50 +02002046/**
2047 * tspp_get_buffer - get TSPP data buffer.
2048 *
2049 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2050 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2051 *
2052 * Return error status
2053 *
2054 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002055const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002056{
Joel Nider5bd73f82011-12-14 16:53:30 +02002057 struct tspp_mem_buffer *buffer;
2058 struct tspp_channel *channel;
2059 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002060
Joel Nider5bd73f82011-12-14 16:53:30 +02002061 if (channel_id >= TSPP_NUM_CHANNELS) {
2062 pr_err("tspp: channel id out of range");
2063 return NULL;
2064 }
2065 pdev = tspp_find_by_id(dev);
2066 if (!pdev) {
2067 pr_err("tspp_get: can't find device %i", dev);
2068 return NULL;
2069 }
2070 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02002071
Joel Nider5bd73f82011-12-14 16:53:30 +02002072 if (!channel->read) {
2073 pr_warn("tspp: no buffer to get on channel %i!",
2074 channel->id);
2075 return NULL;
2076 }
2077
2078 buffer = channel->read;
2079 /* see if we have any buffers ready to read */
2080 if (buffer->state != TSPP_BUF_STATE_DATA)
2081 return 0;
2082
2083 if (buffer->state == TSPP_BUF_STATE_DATA) {
2084 /* mark the buffer as busy */
2085 buffer->state = TSPP_BUF_STATE_LOCKED;
2086
2087 /* increment the pointer along the list */
2088 channel->read = channel->read->next;
2089 }
2090
2091 return &buffer->desc;
2092}
2093EXPORT_SYMBOL(tspp_get_buffer);
2094
Liron Kuch229090d2012-10-30 17:47:50 +02002095/**
2096 * tspp_release_buffer - release TSPP data buffer back to TSPP.
2097 *
2098 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2099 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2100 * @descriptor_id: buffer descriptor ID
2101 *
2102 * Return error status
2103 *
2104 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002105int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
2106{
2107 int i, found = 0;
2108 struct tspp_mem_buffer *buffer;
2109 struct tspp_channel *channel;
2110 struct tspp_device *pdev;
2111
2112 if (channel_id >= TSPP_NUM_CHANNELS) {
2113 pr_err("tspp: channel id out of range");
2114 return -ECHRNG;
2115 }
2116 pdev = tspp_find_by_id(dev);
2117 if (!pdev) {
2118 pr_err("tspp: can't find device %i", dev);
2119 return -ENODEV;
2120 }
2121 channel = &pdev->channels[channel_id];
2122
2123 if (descriptor_id > channel->buffer_count)
2124 pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
2125
2126 /* find the correct descriptor */
2127 buffer = channel->locked;
2128 for (i = 0; i < channel->buffer_count; i++) {
2129 if (buffer->desc.id == descriptor_id) {
2130 found = 1;
2131 break;
2132 }
2133 buffer = buffer->next;
2134 }
2135 channel->locked = channel->locked->next;
2136
2137 if (!found) {
2138 pr_err("tspp: cant find desc %i", descriptor_id);
2139 return -EINVAL;
2140 }
2141
2142 /* make sure the buffer is in the expected state */
2143 if (buffer->state != TSPP_BUF_STATE_LOCKED) {
2144 pr_err("tspp: buffer %i not locked", descriptor_id);
2145 return -EINVAL;
2146 }
2147 /* unlock the buffer and requeue it */
2148 buffer->state = TSPP_BUF_STATE_WAITING;
2149
2150 if (tspp_queue_buffer(channel, buffer))
2151 pr_warn("tspp: can't requeue buffer");
Joel Nider5556a852011-10-16 10:52:13 +02002152 return 0;
2153}
Joel Nider5bd73f82011-12-14 16:53:30 +02002154EXPORT_SYMBOL(tspp_release_buffer);
2155
Liron Kuch229090d2012-10-30 17:47:50 +02002156/**
2157 * tspp_allocate_buffers - allocate TSPP data buffers.
2158 *
2159 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2160 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2161 * @count: number of buffers to allocate
2162 * @size: size of each buffer to allocate
2163 * @int_freq: interrupt frequency
2164 * @alloc: user defined memory allocator function. Pass NULL for default.
2165 * @memfree: user defined memory free function. Pass NULL for default.
2166 * @user: user data to pass to the memory allocator/free function
2167 *
2168 * Return error status
2169 *
2170 * The user can optionally call this function explicitly to allocate the TSPP
2171 * data buffers. Alternatively, if the user did not call this function, it
2172 * is called implicitly by tspp_add_filter().
2173 */
2174int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size,
2175 u32 int_freq, tspp_allocator *alloc,
2176 tspp_memfree *memfree, void *user)
Joel Nider5bd73f82011-12-14 16:53:30 +02002177{
2178 struct tspp_channel *channel;
2179 struct tspp_device *pdev;
2180 struct tspp_mem_buffer *last = NULL;
2181
2182 TSPP_DEBUG("tspp_allocate_buffers");
2183
2184 if (channel_id >= TSPP_NUM_CHANNELS) {
Liron Kuch229090d2012-10-30 17:47:50 +02002185 pr_err("%s: channel id out of range", __func__);
Joel Nider5bd73f82011-12-14 16:53:30 +02002186 return -ECHRNG;
2187 }
Liron Kuch229090d2012-10-30 17:47:50 +02002188
Joel Nider5bd73f82011-12-14 16:53:30 +02002189 pdev = tspp_find_by_id(dev);
2190 if (!pdev) {
Liron Kuch229090d2012-10-30 17:47:50 +02002191 pr_err("%s: can't find device %i", __func__, dev);
Joel Nider5bd73f82011-12-14 16:53:30 +02002192 return -ENODEV;
2193 }
Liron Kuch229090d2012-10-30 17:47:50 +02002194
2195 if (count < MIN_ACCEPTABLE_BUFFER_COUNT) {
2196 pr_err("%s: tspp requires a minimum of %i buffers\n",
2197 __func__, MIN_ACCEPTABLE_BUFFER_COUNT);
2198 return -EINVAL;
2199 }
2200
Hamad Kadmany45f3f062013-05-22 15:40:38 +03002201 if (count > TSPP_NUM_BUFFERS) {
2202 pr_err("%s: tspp requires a maximum of %i buffers\n",
2203 __func__, TSPP_NUM_BUFFERS);
2204 return -EINVAL;
2205 }
2206
Joel Nider5bd73f82011-12-14 16:53:30 +02002207 channel = &pdev->channels[channel_id];
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002208
Liron Kuch229090d2012-10-30 17:47:50 +02002209 /* allow buffer allocation only if there was no previous buffer
2210 * allocation for this channel.
2211 */
2212 if (channel->buffer_count > 0) {
2213 pr_err("%s: buffers already allocated for channel %u",
2214 __func__, channel_id);
2215 return -EINVAL;
2216 }
Joel Nider5bd73f82011-12-14 16:53:30 +02002217
2218 channel->max_buffers = count;
2219
2220 /* set up interrupt frequency */
Liron Kuch229090d2012-10-30 17:47:50 +02002221 if (int_freq > channel->max_buffers) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002222 int_freq = channel->max_buffers;
Liron Kuch229090d2012-10-30 17:47:50 +02002223 pr_warn("%s: setting interrupt frequency to %u\n",
2224 __func__, int_freq);
Joel Nider5bd73f82011-12-14 16:53:30 +02002225 }
Liron Kuch229090d2012-10-30 17:47:50 +02002226 channel->int_freq = int_freq;
2227 /*
2228 * it is the responsibility of the caller to tspp_allocate_buffers(),
2229 * whether it's the user or the driver, to make sure the size parameter
2230 * is compatible to the channel mode.
2231 */
2232 channel->buffer_size = size;
Joel Nider5bd73f82011-12-14 16:53:30 +02002233
Liron Kuch229090d2012-10-30 17:47:50 +02002234 /* save user defined memory free function for later use */
2235 channel->memfree = memfree;
2236 channel->user_info = user;
2237
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002238 /*
2239 * For small buffers, create a DMA pool so that memory
2240 * is not wasted through dma_alloc_coherent.
2241 */
2242 if (TSPP_USE_DMA_POOL(channel->buffer_size)) {
2243 channel->dma_pool = dma_pool_create("tspp",
2244 NULL, channel->buffer_size, 0, 0);
2245 if (!channel->dma_pool) {
2246 pr_err("%s: Can't allocate memory pool\n", __func__);
2247 return -ENOMEM;
2248 }
2249 } else {
2250 channel->dma_pool = NULL;
2251 }
2252
2253
Liron Kuch229090d2012-10-30 17:47:50 +02002254 for (channel->buffer_count = 0;
2255 channel->buffer_count < channel->max_buffers;
Joel Nider5bd73f82011-12-14 16:53:30 +02002256 channel->buffer_count++) {
2257
2258 /* allocate the descriptor */
2259 struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
2260 kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
2261 if (!desc) {
Liron Kuch229090d2012-10-30 17:47:50 +02002262 pr_warn("%s: Can't allocate desc %i",
2263 __func__, channel->buffer_count);
Joel Nider5bd73f82011-12-14 16:53:30 +02002264 break;
2265 }
2266
2267 desc->desc.id = channel->buffer_count;
2268 /* allocate the buffer */
2269 if (tspp_alloc_buffer(channel_id, &desc->desc,
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002270 channel->buffer_size, channel->dma_pool,
2271 alloc, user) != 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002272 kfree(desc);
Liron Kuch229090d2012-10-30 17:47:50 +02002273 pr_warn("%s: Can't allocate buffer %i",
2274 __func__, channel->buffer_count);
Joel Nider5bd73f82011-12-14 16:53:30 +02002275 break;
2276 }
2277
2278 /* add the descriptor to the list */
2279 desc->filled = 0;
2280 desc->read_index = 0;
2281 if (!channel->data) {
2282 channel->data = desc;
2283 desc->next = channel->data;
2284 } else {
2285 last->next = desc;
2286 }
2287 last = desc;
2288 desc->next = channel->data;
2289
2290 /* prepare the sps descriptor */
2291 desc->sps.phys_base = desc->desc.phys_base;
2292 desc->sps.base = desc->desc.virt_base;
2293 desc->sps.size = desc->desc.size;
2294
2295 /* start the transfer */
2296 if (tspp_queue_buffer(channel, desc))
Liron Kuch229090d2012-10-30 17:47:50 +02002297 pr_err("%s: can't queue buffer %i",
2298 __func__, desc->desc.id);
2299 }
2300
2301 if (channel->buffer_count < channel->max_buffers) {
2302 /*
2303 * we failed to allocate the requested number of buffers.
2304 * we don't allow a partial success, so need to clean up here.
2305 */
2306 tspp_destroy_buffers(channel_id, channel);
2307 channel->buffer_count = 0;
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002308
2309 if (channel->dma_pool) {
2310 dma_pool_destroy(channel->dma_pool);
2311 channel->dma_pool = NULL;
2312 }
Liron Kuch229090d2012-10-30 17:47:50 +02002313 return -ENOMEM;
Joel Nider5bd73f82011-12-14 16:53:30 +02002314 }
2315
2316 channel->waiting = channel->data;
2317 channel->read = channel->data;
2318 channel->locked = channel->data;
Liron Kuch229090d2012-10-30 17:47:50 +02002319
Hamad Kadmany567bed82012-11-29 14:15:57 +02002320 /* Now that buffers are scheduled to HW, kick data expiration timer */
2321 if (channel->expiration_period_ms)
2322 mod_timer(&channel->expiration_timer,
2323 jiffies +
2324 MSEC_TO_JIFFIES(
2325 channel->expiration_period_ms));
2326
Joel Nider5bd73f82011-12-14 16:53:30 +02002327 return 0;
2328}
2329EXPORT_SYMBOL(tspp_allocate_buffers);
Joel Nider5556a852011-10-16 10:52:13 +02002330
2331/*** File Operations ***/
2332static ssize_t tspp_open(struct inode *inode, struct file *filp)
2333{
Joel Nider5bd73f82011-12-14 16:53:30 +02002334 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002335 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02002336
2337 TSPP_DEBUG("tspp_open");
Joel Nider5556a852011-10-16 10:52:13 +02002338 channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
2339 filp->private_data = channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02002340 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002341
2342 /* if this channel is already in use, quit */
2343 if (channel->used) {
2344 pr_err("tspp channel %i already in use",
2345 MINOR(channel->cdev.dev));
2346 return -EACCES;
2347 }
2348
Joel Nider5bd73f82011-12-14 16:53:30 +02002349 if (tspp_open_channel(dev, channel->id) != 0) {
Joel Nider5556a852011-10-16 10:52:13 +02002350 pr_err("tspp: error opening channel");
2351 return -EACCES;
2352 }
2353
2354 return 0;
2355}
2356
2357static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
2358{
2359 unsigned long flags;
2360 unsigned int mask = 0;
2361 struct tspp_channel *channel;
2362 channel = filp->private_data;
2363
2364 /* register the wait queue for this channel */
2365 poll_wait(filp, &channel->in_queue, p);
2366
2367 spin_lock_irqsave(&channel->pdev->spinlock, flags);
Joel Nider5bd73f82011-12-14 16:53:30 +02002368 if (channel->read &&
2369 channel->read->state == TSPP_BUF_STATE_DATA)
Joel Nider5556a852011-10-16 10:52:13 +02002370 mask = POLLIN | POLLRDNORM;
2371
2372 spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
2373
2374 return mask;
2375}
2376
2377static ssize_t tspp_release(struct inode *inode, struct file *filp)
2378{
Joel Nider5bd73f82011-12-14 16:53:30 +02002379 struct tspp_channel *channel = filp->private_data;
2380 u32 dev = channel->pdev->pdev->id;
2381 TSPP_DEBUG("tspp_release");
Joel Nider5556a852011-10-16 10:52:13 +02002382
Joel Nider5bd73f82011-12-14 16:53:30 +02002383 tspp_close_channel(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02002384
2385 return 0;
2386}
2387
2388static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
2389 loff_t *f_pos)
2390{
2391 size_t size = 0;
2392 size_t transferred = 0;
2393 struct tspp_channel *channel;
2394 struct tspp_mem_buffer *buffer;
2395 channel = filp->private_data;
2396
2397 TSPP_DEBUG("tspp_read");
Joel Nider5bd73f82011-12-14 16:53:30 +02002398
2399 while (!channel->read) {
2400 if (filp->f_flags & O_NONBLOCK) {
2401 pr_warn("tspp: no buffer on channel %i!",
2402 channel->id);
2403 return -EAGAIN;
2404 }
2405 /* go to sleep if there is nothing to read */
2406 if (wait_event_interruptible(channel->in_queue,
2407 (channel->read != NULL))) {
2408 pr_err("tspp: rude awakening\n");
2409 return -ERESTARTSYS;
2410 }
2411 }
2412
2413 buffer = channel->read;
2414
Joel Nider5556a852011-10-16 10:52:13 +02002415 /* see if we have any buffers ready to read */
2416 while (buffer->state != TSPP_BUF_STATE_DATA) {
2417 if (filp->f_flags & O_NONBLOCK) {
2418 pr_warn("tspp: nothing to read on channel %i!",
2419 channel->id);
2420 return -EAGAIN;
2421 }
2422 /* go to sleep if there is nothing to read */
Joel Nider5556a852011-10-16 10:52:13 +02002423 if (wait_event_interruptible(channel->in_queue,
2424 (buffer->state == TSPP_BUF_STATE_DATA))) {
2425 pr_err("tspp: rude awakening\n");
2426 return -ERESTARTSYS;
2427 }
2428 }
2429
2430 while (buffer->state == TSPP_BUF_STATE_DATA) {
2431 size = min(count, buffer->filled);
Joel Nider5556a852011-10-16 10:52:13 +02002432 if (size == 0)
2433 break;
2434
Joel Nider5bd73f82011-12-14 16:53:30 +02002435 if (copy_to_user(buf, buffer->desc.virt_base +
Joel Nider5556a852011-10-16 10:52:13 +02002436 buffer->read_index, size)) {
2437 pr_err("tspp: error copying to user buffer");
Joel Nider5bd73f82011-12-14 16:53:30 +02002438 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02002439 }
2440 buf += size;
2441 count -= size;
2442 transferred += size;
2443 buffer->read_index += size;
2444
Liron Kuch229090d2012-10-30 17:47:50 +02002445 /*
2446 * after reading the end of the buffer, requeue it,
2447 * and set up for reading the next one
2448 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002449 if (buffer->read_index == buffer->filled) {
Joel Nider5556a852011-10-16 10:52:13 +02002450 buffer->state = TSPP_BUF_STATE_WAITING;
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002451
Joel Nider5bd73f82011-12-14 16:53:30 +02002452 if (tspp_queue_buffer(channel, buffer))
2453 pr_err("tspp: can't submit transfer");
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002454
Joel Nider5bd73f82011-12-14 16:53:30 +02002455 channel->locked = channel->read;
2456 channel->read = channel->read->next;
Joel Nider5556a852011-10-16 10:52:13 +02002457 }
2458 }
2459
2460 return transferred;
2461}
2462
2463static long tspp_ioctl(struct file *filp,
2464 unsigned int param0, unsigned long param1)
2465{
Joel Nider5bd73f82011-12-14 16:53:30 +02002466 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002467 int rc = -1;
2468 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02002469 struct tspp_select_source ss;
2470 struct tspp_filter f;
2471 struct tspp_key k;
2472 struct tspp_iv iv;
2473 struct tspp_system_keys sk;
2474 struct tspp_buffer b;
Joel Nider5556a852011-10-16 10:52:13 +02002475 channel = filp->private_data;
Joel Nider5bd73f82011-12-14 16:53:30 +02002476 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002477
Liron Kuchc2392df2013-02-14 16:26:38 +02002478 if ((param0 != TSPP_IOCTL_CLOSE_STREAM) && !param1)
Joel Nider5556a852011-10-16 10:52:13 +02002479 return -EINVAL;
2480
2481 switch (param0) {
2482 case TSPP_IOCTL_SELECT_SOURCE:
Joel Nider5bd73f82011-12-14 16:53:30 +02002483 if (!access_ok(VERIFY_READ, param1,
2484 sizeof(struct tspp_select_source))) {
2485 return -EBUSY;
2486 }
2487 if (__copy_from_user(&ss, (void *)param1,
2488 sizeof(struct tspp_select_source)) == 0)
2489 rc = tspp_select_source(dev, channel->id, &ss);
Joel Nider5556a852011-10-16 10:52:13 +02002490 break;
2491 case TSPP_IOCTL_ADD_FILTER:
Joel Nider5bd73f82011-12-14 16:53:30 +02002492 if (!access_ok(VERIFY_READ, param1,
2493 sizeof(struct tspp_filter))) {
2494 return -ENOSR;
2495 }
2496 if (__copy_from_user(&f, (void *)param1,
2497 sizeof(struct tspp_filter)) == 0)
2498 rc = tspp_add_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002499 break;
2500 case TSPP_IOCTL_REMOVE_FILTER:
Joel Nider5bd73f82011-12-14 16:53:30 +02002501 if (!access_ok(VERIFY_READ, param1,
2502 sizeof(struct tspp_filter))) {
2503 return -EBUSY;
2504 }
2505 if (__copy_from_user(&f, (void *)param1,
2506 sizeof(struct tspp_filter)) == 0)
2507 rc = tspp_remove_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002508 break;
2509 case TSPP_IOCTL_SET_KEY:
Joel Nider5bd73f82011-12-14 16:53:30 +02002510 if (!access_ok(VERIFY_READ, param1,
2511 sizeof(struct tspp_key))) {
2512 return -EBUSY;
2513 }
2514 if (__copy_from_user(&k, (void *)param1,
2515 sizeof(struct tspp_key)) == 0)
2516 rc = tspp_set_key(dev, channel->id, &k);
Joel Nider5556a852011-10-16 10:52:13 +02002517 break;
2518 case TSPP_IOCTL_SET_IV:
Joel Nider5bd73f82011-12-14 16:53:30 +02002519 if (!access_ok(VERIFY_READ, param1,
2520 sizeof(struct tspp_iv))) {
2521 return -EBUSY;
2522 }
2523 if (__copy_from_user(&iv, (void *)param1,
2524 sizeof(struct tspp_iv)) == 0)
2525 rc = tspp_set_iv(channel, &iv);
Joel Nider5556a852011-10-16 10:52:13 +02002526 break;
2527 case TSPP_IOCTL_SET_SYSTEM_KEYS:
Joel Nider5bd73f82011-12-14 16:53:30 +02002528 if (!access_ok(VERIFY_READ, param1,
2529 sizeof(struct tspp_system_keys))) {
2530 return -EINVAL;
2531 }
2532 if (__copy_from_user(&sk, (void *)param1,
2533 sizeof(struct tspp_system_keys)) == 0)
2534 rc = tspp_set_system_keys(channel, &sk);
Joel Nider5556a852011-10-16 10:52:13 +02002535 break;
2536 case TSPP_IOCTL_BUFFER_SIZE:
Joel Nider5bd73f82011-12-14 16:53:30 +02002537 if (!access_ok(VERIFY_READ, param1,
2538 sizeof(struct tspp_buffer))) {
2539 rc = -EINVAL;
2540 }
2541 if (__copy_from_user(&b, (void *)param1,
2542 sizeof(struct tspp_buffer)) == 0)
2543 rc = tspp_set_buffer_size(channel, &b);
Joel Nider5556a852011-10-16 10:52:13 +02002544 break;
Liron Kuchc2392df2013-02-14 16:26:38 +02002545 case TSPP_IOCTL_CLOSE_STREAM:
2546 rc = tspp_close_stream(dev, channel->id);
2547 break;
Joel Nider5556a852011-10-16 10:52:13 +02002548 default:
2549 pr_err("tspp: Unknown ioctl %i", param0);
2550 }
2551
Liron Kuch229090d2012-10-30 17:47:50 +02002552 /*
2553 * normalize the return code in case one of the subfunctions does
2554 * something weird
2555 */
Joel Nider5556a852011-10-16 10:52:13 +02002556 if (rc != 0)
Joel Nider5bd73f82011-12-14 16:53:30 +02002557 rc = -ENOIOCTLCMD;
Joel Nider5556a852011-10-16 10:52:13 +02002558
2559 return rc;
2560}
2561
2562/*** debugfs ***/
Joel Nider5556a852011-10-16 10:52:13 +02002563static int debugfs_iomem_x32_set(void *data, u64 val)
2564{
2565 writel_relaxed(val, data);
2566 wmb();
2567 return 0;
2568}
2569
2570static int debugfs_iomem_x32_get(void *data, u64 *val)
2571{
2572 *val = readl_relaxed(data);
2573 return 0;
2574}
2575
2576DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
2577 debugfs_iomem_x32_set, "0x%08llx");
2578
2579static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
2580 int instance)
2581{
2582 char name[10];
2583 snprintf(name, 10, "tsif%i", instance);
2584 tsif_device->dent_tsif = debugfs_create_dir(
2585 name, NULL);
2586 if (tsif_device->dent_tsif) {
2587 int i;
2588 void __iomem *base = tsif_device->base;
2589 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
2590 tsif_device->debugfs_tsif_regs[i] =
2591 debugfs_create_file(
2592 debugfs_tsif_regs[i].name,
2593 debugfs_tsif_regs[i].mode,
2594 tsif_device->dent_tsif,
2595 base + debugfs_tsif_regs[i].offset,
2596 &fops_iomem_x32);
2597 }
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02002598
2599 debugfs_create_u32(
2600 "stat_rx_chunks",
Hamad Kadmany8c6c8972013-03-28 08:23:26 +02002601 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02002602 tsif_device->dent_tsif,
2603 &tsif_device->stat_rx);
2604
2605 debugfs_create_u32(
2606 "stat_overflow",
Hamad Kadmany8c6c8972013-03-28 08:23:26 +02002607 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02002608 tsif_device->dent_tsif,
2609 &tsif_device->stat_overflow);
2610
2611 debugfs_create_u32(
2612 "stat_lost_sync",
Hamad Kadmany8c6c8972013-03-28 08:23:26 +02002613 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02002614 tsif_device->dent_tsif,
2615 &tsif_device->stat_lost_sync);
2616
2617 debugfs_create_u32(
2618 "stat_timeout",
Hamad Kadmany8c6c8972013-03-28 08:23:26 +02002619 S_IRUGO | S_IWUSR | S_IWGRP,
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02002620 tsif_device->dent_tsif,
2621 &tsif_device->stat_timeout);
Joel Nider5556a852011-10-16 10:52:13 +02002622 }
2623}
2624
2625static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
2626{
2627 if (tsif_device->dent_tsif) {
2628 int i;
2629 debugfs_remove_recursive(tsif_device->dent_tsif);
2630 tsif_device->dent_tsif = NULL;
2631 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
2632 tsif_device->debugfs_tsif_regs[i] = NULL;
2633 }
2634}
2635
2636static void tspp_debugfs_init(struct tspp_device *device, int instance)
2637{
2638 char name[10];
2639 snprintf(name, 10, "tspp%i", instance);
2640 device->dent = debugfs_create_dir(
2641 name, NULL);
2642 if (device->dent) {
2643 int i;
2644 void __iomem *base = device->base;
2645 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
2646 device->debugfs_regs[i] =
2647 debugfs_create_file(
2648 debugfs_tspp_regs[i].name,
2649 debugfs_tspp_regs[i].mode,
2650 device->dent,
2651 base + debugfs_tspp_regs[i].offset,
2652 &fops_iomem_x32);
2653 }
2654 }
2655}
2656
2657static void tspp_debugfs_exit(struct tspp_device *device)
2658{
2659 if (device->dent) {
2660 int i;
2661 debugfs_remove_recursive(device->dent);
2662 device->dent = NULL;
2663 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
2664 device->debugfs_regs[i] = NULL;
2665 }
2666}
Joel Nider5556a852011-10-16 10:52:13 +02002667
Liron Kuch8fa85b02013-01-01 18:29:47 +02002668/* copy device-tree data to platfrom data struct */
2669static __devinit struct msm_tspp_platform_data *
2670msm_tspp_dt_to_pdata(struct platform_device *pdev)
2671{
2672 struct device_node *node = pdev->dev.of_node;
2673 struct msm_tspp_platform_data *data;
2674 struct msm_gpio *gpios;
2675 int i, rc;
2676 int gpio;
2677 u32 gpio_func;
2678
2679 /* Note: memory allocated by devm_kzalloc is freed automatically */
2680 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2681 if (!data) {
2682 pr_err("tspp: Unable to allocate platform data\n");
2683 return NULL;
2684 }
2685 rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk);
2686 if (rc) {
2687 pr_err("tspp: Could not find tsif-pclk property, err = %d\n",
2688 rc);
2689 return NULL;
2690 }
2691 rc = of_property_read_string(node, "qcom,tsif-ref-clk",
2692 &data->tsif_ref_clk);
2693 if (rc) {
2694 pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n",
2695 rc);
2696 return NULL;
2697 }
2698
2699 data->num_gpios = of_gpio_count(node);
2700 if (data->num_gpios == 0) {
2701 pr_err("tspp: Could not find GPIO definitions\n");
2702 return NULL;
2703 }
2704 gpios = devm_kzalloc(&pdev->dev,
2705 (data->num_gpios * sizeof(struct msm_gpio)),
2706 GFP_KERNEL);
2707 if (!gpios) {
2708 pr_err("tspp: Unable to allocate memory for GPIOs table\n");
2709 return NULL;
2710 }
2711 /* Assuming GPIO FUNC property is the same for all GPIOs */
2712 if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) {
2713 pr_err("tspp: Could not find gpios-func property\n");
2714 return NULL;
2715 }
2716 for (i = 0; i < data->num_gpios; i++) {
2717 gpio = of_get_gpio(node, i);
2718 gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func,
2719 GPIO_CFG_INPUT,
2720 GPIO_CFG_PULL_DOWN,
2721 GPIO_CFG_2MA);
2722 rc = of_property_read_string_index(node, "qcom,gpio-names",
2723 i, &gpios[i].label);
2724 if (rc)
2725 pr_warn("tspp: Could not find gpio-names property\n");
2726 }
2727
2728 data->gpios = gpios;
2729
2730 return data;
2731}
2732
2733static int msm_tspp_map_irqs(struct platform_device *pdev,
2734 struct tspp_device *device)
2735{
2736 int rc;
2737 int i;
2738
2739 /* get IRQ numbers from platform information */
2740
2741 /* map TSPP IRQ */
2742 rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ");
2743 if (rc > 0) {
2744 device->tspp_irq = rc;
2745 rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
2746 dev_name(&pdev->dev), device);
2747 if (rc) {
2748 dev_err(&pdev->dev,
2749 "failed to request TSPP IRQ %d : %d",
2750 device->tspp_irq, rc);
2751 device->tspp_irq = 0;
2752 return -EINVAL;
2753 }
2754 } else {
2755 dev_err(&pdev->dev, "failed to get TSPP IRQ");
2756 return -EINVAL;
2757 }
2758
2759 /* map TSIF IRQs */
2760 rc = platform_get_irq_byname(pdev, "TSIF0_IRQ");
2761 if (rc > 0) {
2762 device->tsif[0].tsif_irq = rc;
2763 } else {
2764 dev_err(&pdev->dev, "failed to get TSIF0 IRQ");
2765 return -EINVAL;
2766 }
2767
2768 rc = platform_get_irq_byname(pdev, "TSIF1_IRQ");
2769 if (rc > 0) {
2770 device->tsif[1].tsif_irq = rc;
2771 } else {
2772 dev_err(&pdev->dev, "failed to get TSIF1 IRQ");
2773 return -EINVAL;
2774 }
2775
2776 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2777 rc = request_irq(device->tsif[i].tsif_irq,
2778 tsif_isr, IRQF_SHARED,
2779 dev_name(&pdev->dev), &device->tsif[i]);
2780 if (rc) {
2781 dev_warn(&pdev->dev, "failed to request TSIF%d IRQ: %d",
2782 i, rc);
2783 device->tsif[i].tsif_irq = 0;
2784 }
2785 }
2786
2787 /* map BAM IRQ */
2788 rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ");
2789 if (rc > 0) {
2790 device->bam_irq = rc;
2791 } else {
2792 dev_err(&pdev->dev, "failed to get TSPP BAM IRQ");
2793 return -EINVAL;
2794 }
2795
2796 return 0;
2797}
2798
Joel Nider5556a852011-10-16 10:52:13 +02002799static int __devinit msm_tspp_probe(struct platform_device *pdev)
2800{
2801 int rc = -ENODEV;
2802 u32 version;
Liron Kuch229090d2012-10-30 17:47:50 +02002803 u32 i, j;
Joel Nider5556a852011-10-16 10:52:13 +02002804 struct msm_tspp_platform_data *data;
2805 struct tspp_device *device;
2806 struct resource *mem_tsif0;
2807 struct resource *mem_tsif1;
2808 struct resource *mem_tspp;
2809 struct resource *mem_bam;
Liron Kuch229090d2012-10-30 17:47:50 +02002810 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02002811
Liron Kuch8fa85b02013-01-01 18:29:47 +02002812 if (pdev->dev.of_node) {
2813 /* get information from device tree */
2814 data = msm_tspp_dt_to_pdata(pdev);
2815 /* get device ID */
2816 rc = of_property_read_u32(pdev->dev.of_node,
2817 "cell-index", &pdev->id);
2818 if (rc)
2819 pdev->id = -1;
2820
2821 pdev->dev.platform_data = data;
2822 } else {
2823 /* must have platform data */
2824 data = pdev->dev.platform_data;
2825 }
Joel Nider5556a852011-10-16 10:52:13 +02002826 if (!data) {
2827 pr_err("tspp: Platform data not available");
2828 rc = -EINVAL;
2829 goto out;
2830 }
2831
2832 /* check for valid device id */
Joel Nider5bd73f82011-12-14 16:53:30 +02002833 if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
Joel Nider5556a852011-10-16 10:52:13 +02002834 pr_err("tspp: Invalid device ID %d", pdev->id);
2835 rc = -EINVAL;
2836 goto out;
2837 }
2838
2839 /* OK, we will use this device */
2840 device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
2841 if (!device) {
2842 pr_err("tspp: Failed to allocate memory for device");
2843 rc = -ENOMEM;
2844 goto out;
2845 }
2846
2847 /* set up references */
2848 device->pdev = pdev;
2849 platform_set_drvdata(pdev, device);
2850
2851 /* map clocks */
2852 if (data->tsif_pclk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002853 device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
Joel Nider5556a852011-10-16 10:52:13 +02002854 if (IS_ERR(device->tsif_pclk)) {
2855 pr_err("tspp: failed to get %s",
2856 data->tsif_pclk);
2857 rc = PTR_ERR(device->tsif_pclk);
2858 device->tsif_pclk = NULL;
2859 goto err_pclock;
2860 }
2861 }
2862 if (data->tsif_ref_clk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002863 device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
Joel Nider5556a852011-10-16 10:52:13 +02002864 if (IS_ERR(device->tsif_ref_clk)) {
2865 pr_err("tspp: failed to get %s",
2866 data->tsif_ref_clk);
2867 rc = PTR_ERR(device->tsif_ref_clk);
2868 device->tsif_ref_clk = NULL;
2869 goto err_refclock;
2870 }
2871 }
2872
2873 /* map I/O memory */
Liron Kuch8fa85b02013-01-01 18:29:47 +02002874 mem_tsif0 = platform_get_resource_byname(pdev,
2875 IORESOURCE_MEM, "MSM_TSIF0_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002876 if (!mem_tsif0) {
2877 pr_err("tspp: Missing tsif0 MEM resource");
2878 rc = -ENXIO;
2879 goto err_res_tsif0;
2880 }
2881 device->tsif[0].base = ioremap(mem_tsif0->start,
2882 resource_size(mem_tsif0));
2883 if (!device->tsif[0].base) {
2884 pr_err("tspp: ioremap failed");
2885 goto err_map_tsif0;
2886 }
2887
Liron Kuch8fa85b02013-01-01 18:29:47 +02002888 mem_tsif1 = platform_get_resource_byname(pdev,
2889 IORESOURCE_MEM, "MSM_TSIF1_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002890 if (!mem_tsif1) {
2891 dev_err(&pdev->dev, "Missing tsif1 MEM resource");
2892 rc = -ENXIO;
2893 goto err_res_tsif1;
2894 }
2895 device->tsif[1].base = ioremap(mem_tsif1->start,
2896 resource_size(mem_tsif1));
2897 if (!device->tsif[1].base) {
2898 dev_err(&pdev->dev, "ioremap failed");
2899 goto err_map_tsif1;
2900 }
2901
Liron Kuch8fa85b02013-01-01 18:29:47 +02002902 mem_tspp = platform_get_resource_byname(pdev,
2903 IORESOURCE_MEM, "MSM_TSPP_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002904 if (!mem_tspp) {
2905 dev_err(&pdev->dev, "Missing MEM resource");
2906 rc = -ENXIO;
2907 goto err_res_dev;
2908 }
2909 device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
2910 if (!device->base) {
2911 dev_err(&pdev->dev, "ioremap failed");
2912 goto err_map_dev;
2913 }
2914
Liron Kuch8fa85b02013-01-01 18:29:47 +02002915 mem_bam = platform_get_resource_byname(pdev,
2916 IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002917 if (!mem_bam) {
2918 pr_err("tspp: Missing bam MEM resource");
2919 rc = -ENXIO;
2920 goto err_res_bam;
2921 }
2922 memset(&device->bam_props, 0, sizeof(device->bam_props));
2923 device->bam_props.phys_addr = mem_bam->start;
2924 device->bam_props.virt_addr = ioremap(mem_bam->start,
2925 resource_size(mem_bam));
2926 if (!device->bam_props.virt_addr) {
2927 dev_err(&pdev->dev, "ioremap failed");
2928 goto err_map_bam;
2929 }
2930
Liron Kuch8fa85b02013-01-01 18:29:47 +02002931 if (msm_tspp_map_irqs(pdev, device))
Joel Nider5556a852011-10-16 10:52:13 +02002932 goto err_irq;
Joel Nider5556a852011-10-16 10:52:13 +02002933
Joel Nider5556a852011-10-16 10:52:13 +02002934 /* power management */
2935 pm_runtime_set_active(&pdev->dev);
2936 pm_runtime_enable(&pdev->dev);
2937
Joel Nider5556a852011-10-16 10:52:13 +02002938 tspp_debugfs_init(device, 0);
2939
2940 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2941 tsif_debugfs_init(&device->tsif[i], i);
Joel Nider5556a852011-10-16 10:52:13 +02002942
2943 wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
2944 dev_name(&pdev->dev));
2945
2946 /* set up pointers to ram-based 'registers' */
Joel Nider5bd73f82011-12-14 16:53:30 +02002947 device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
2948 device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
2949 device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
2950 device->tspp_key_table = device->base + TSPP_DATA_KEY;
2951 device->tspp_global_performance =
2952 device->base + TSPP_GLOBAL_PERFORMANCE;
2953 device->tspp_pipe_context =
2954 device->base + TSPP_PIPE_CONTEXT;
2955 device->tspp_pipe_performance =
2956 device->base + TSPP_PIPE_PERFORMANCE;
Joel Nider5556a852011-10-16 10:52:13 +02002957
2958 device->bam_props.summing_threshold = 0x10;
2959 device->bam_props.irq = device->bam_irq;
2960 device->bam_props.manage = SPS_BAM_MGR_LOCAL;
2961
Liron Kuch8fa85b02013-01-01 18:29:47 +02002962 if (tspp_clock_start(device) != 0) {
2963 dev_err(&pdev->dev, "Can't start clocks");
2964 goto err_clock;
2965 }
2966
Joel Nider5556a852011-10-16 10:52:13 +02002967 if (sps_register_bam_device(&device->bam_props,
2968 &device->bam_handle) != 0) {
2969 pr_err("tspp: failed to register bam");
2970 goto err_bam;
2971 }
2972
Joel Nider5556a852011-10-16 10:52:13 +02002973 spin_lock_init(&device->spinlock);
2974 tasklet_init(&device->tlet, tspp_sps_complete_tlet,
2975 (unsigned long)device);
2976
2977 /* initialize everything to a known state */
2978 tspp_global_reset(device);
2979
2980 version = readl_relaxed(device->base + TSPP_VERSION);
Liron Kuch8fa85b02013-01-01 18:29:47 +02002981 /*
2982 * TSPP version can be bits [7:0] or alternatively,
2983 * TSPP major version is bits [31:28].
2984 */
2985 if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1))
Joel Nider5556a852011-10-16 10:52:13 +02002986 pr_warn("tspp: unrecognized hw version=%i", version);
2987
Joel Nider5bd73f82011-12-14 16:53:30 +02002988 /* initialize the channels */
Joel Nider5556a852011-10-16 10:52:13 +02002989 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002990 if (tspp_channel_init(&(device->channels[i]), device) != 0) {
2991 pr_err("tspp_channel_init failed");
2992 goto err_channel;
2993 }
Joel Nider5556a852011-10-16 10:52:13 +02002994 }
2995
Joel Nider5bd73f82011-12-14 16:53:30 +02002996 /* stop the clocks for power savings */
2997 tspp_clock_stop(device);
2998
2999 /* everything is ok, so add the device to the list */
3000 list_add_tail(&(device->devlist), &tspp_devices);
3001
Joel Nider5556a852011-10-16 10:52:13 +02003002 return 0;
3003
Joel Nider5bd73f82011-12-14 16:53:30 +02003004err_channel:
Liron Kuch8fa85b02013-01-01 18:29:47 +02003005 /* un-initialize channels */
Liron Kuch229090d2012-10-30 17:47:50 +02003006 for (j = 0; j < i; j++) {
3007 channel = &(device->channels[i]);
3008 device_destroy(tspp_class, channel->cdev.dev);
3009 cdev_del(&channel->cdev);
3010 }
Liron Kuch8fa85b02013-01-01 18:29:47 +02003011
Joel Nider5556a852011-10-16 10:52:13 +02003012 sps_deregister_bam_device(device->bam_handle);
Liron Kuch8fa85b02013-01-01 18:29:47 +02003013err_clock:
Joel Nider5556a852011-10-16 10:52:13 +02003014err_bam:
Joel Nider5556a852011-10-16 10:52:13 +02003015 tspp_debugfs_exit(device);
3016 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3017 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02003018err_irq:
Liron Kuch8fa85b02013-01-01 18:29:47 +02003019 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
3020 if (device->tsif[i].tsif_irq)
3021 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
3022 }
3023 if (device->tspp_irq)
3024 free_irq(device->tspp_irq, device);
3025
Joel Nider5556a852011-10-16 10:52:13 +02003026 iounmap(device->bam_props.virt_addr);
3027err_map_bam:
3028err_res_bam:
3029 iounmap(device->base);
3030err_map_dev:
3031err_res_dev:
3032 iounmap(device->tsif[1].base);
3033err_map_tsif1:
3034err_res_tsif1:
3035 iounmap(device->tsif[0].base);
3036err_map_tsif0:
3037err_res_tsif0:
3038 if (device->tsif_ref_clk)
3039 clk_put(device->tsif_ref_clk);
3040err_refclock:
3041 if (device->tsif_pclk)
3042 clk_put(device->tsif_pclk);
3043err_pclock:
3044 kfree(device);
3045
3046out:
3047 return rc;
3048}
3049
3050static int __devexit msm_tspp_remove(struct platform_device *pdev)
3051{
Joel Nider5bd73f82011-12-14 16:53:30 +02003052 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02003053 u32 i;
Liron Kuchde8cbf92013-02-21 14:25:57 +02003054 int rc;
Joel Nider5556a852011-10-16 10:52:13 +02003055
3056 struct tspp_device *device = platform_get_drvdata(pdev);
3057
Joel Nider5bd73f82011-12-14 16:53:30 +02003058 /* free the buffers, and delete the channels */
3059 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
3060 channel = &device->channels[i];
3061 tspp_close_channel(device->pdev->id, i);
3062 device_destroy(tspp_class, channel->cdev.dev);
3063 cdev_del(&channel->cdev);
3064 }
3065
Liron Kuch8fa85b02013-01-01 18:29:47 +02003066 /* de-registering BAM device requires clocks */
Liron Kuchde8cbf92013-02-21 14:25:57 +02003067 rc = tspp_clock_start(device);
3068 if (rc == 0) {
3069 sps_deregister_bam_device(device->bam_handle);
3070 tspp_clock_stop(device);
3071 }
Joel Nider5556a852011-10-16 10:52:13 +02003072
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02003073 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
Joel Nider5556a852011-10-16 10:52:13 +02003074 tsif_debugfs_exit(&device->tsif[i]);
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02003075 if (device->tsif[i].tsif_irq)
3076 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
3077 }
Joel Nider5556a852011-10-16 10:52:13 +02003078
3079 wake_lock_destroy(&device->wake_lock);
3080 free_irq(device->tspp_irq, device);
Joel Nider5556a852011-10-16 10:52:13 +02003081
3082 iounmap(device->bam_props.virt_addr);
3083 iounmap(device->base);
3084 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3085 iounmap(device->tsif[i].base);
3086
3087 if (device->tsif_ref_clk)
3088 clk_put(device->tsif_ref_clk);
3089
3090 if (device->tsif_pclk)
3091 clk_put(device->tsif_pclk);
3092
3093 pm_runtime_disable(&pdev->dev);
Liron Kuchde8cbf92013-02-21 14:25:57 +02003094
Joel Nider5556a852011-10-16 10:52:13 +02003095 kfree(device);
3096
3097 return 0;
3098}
3099
3100/*** power management ***/
3101
3102static int tspp_runtime_suspend(struct device *dev)
3103{
3104 dev_dbg(dev, "pm_runtime: suspending...");
3105 return 0;
3106}
3107
3108static int tspp_runtime_resume(struct device *dev)
3109{
3110 dev_dbg(dev, "pm_runtime: resuming...");
3111 return 0;
3112}
3113
3114static const struct dev_pm_ops tspp_dev_pm_ops = {
3115 .runtime_suspend = tspp_runtime_suspend,
3116 .runtime_resume = tspp_runtime_resume,
3117};
3118
Liron Kuch8fa85b02013-01-01 18:29:47 +02003119static struct of_device_id msm_match_table[] = {
3120 {.compatible = "qcom,msm_tspp"},
3121 {}
3122};
3123
Joel Nider5556a852011-10-16 10:52:13 +02003124static struct platform_driver msm_tspp_driver = {
3125 .probe = msm_tspp_probe,
3126 .remove = __exit_p(msm_tspp_remove),
3127 .driver = {
3128 .name = "msm_tspp",
3129 .pm = &tspp_dev_pm_ops,
Liron Kuch8fa85b02013-01-01 18:29:47 +02003130 .of_match_table = msm_match_table,
Joel Nider5556a852011-10-16 10:52:13 +02003131 },
3132};
3133
3134
3135static int __init mod_init(void)
3136{
Joel Nider5556a852011-10-16 10:52:13 +02003137 int rc;
3138
Joel Nider5bd73f82011-12-14 16:53:30 +02003139 /* make the char devs (channels) */
Joel Nider5556a852011-10-16 10:52:13 +02003140 rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
3141 if (rc) {
3142 pr_err("tspp: alloc_chrdev_region failed: %d", rc);
3143 goto err_devrgn;
3144 }
3145
3146 tspp_class = class_create(THIS_MODULE, "tspp");
3147 if (IS_ERR(tspp_class)) {
3148 rc = PTR_ERR(tspp_class);
3149 pr_err("tspp: Error creating class: %d", rc);
3150 goto err_class;
3151 }
3152
Joel Nider5bd73f82011-12-14 16:53:30 +02003153 /* register the driver, and check hardware */
3154 rc = platform_driver_register(&msm_tspp_driver);
3155 if (rc) {
3156 pr_err("tspp: platform_driver_register failed: %d", rc);
3157 goto err_register;
Joel Nider5556a852011-10-16 10:52:13 +02003158 }
3159
3160 return 0;
3161
Joel Nider5bd73f82011-12-14 16:53:30 +02003162err_register:
3163 class_destroy(tspp_class);
Joel Nider5556a852011-10-16 10:52:13 +02003164err_class:
3165 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
3166err_devrgn:
Joel Nider5556a852011-10-16 10:52:13 +02003167 return rc;
3168}
3169
3170static void __exit mod_exit(void)
3171{
Joel Nider5bd73f82011-12-14 16:53:30 +02003172 /* delete low level driver */
3173 platform_driver_unregister(&msm_tspp_driver);
Joel Nider5556a852011-10-16 10:52:13 +02003174
Joel Nider5bd73f82011-12-14 16:53:30 +02003175 /* delete upper layer interface */
Joel Nider5556a852011-10-16 10:52:13 +02003176 class_destroy(tspp_class);
3177 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
Joel Nider5556a852011-10-16 10:52:13 +02003178}
3179
3180module_init(mod_init);
3181module_exit(mod_exit);
3182
Joel Nider5bd73f82011-12-14 16:53:30 +02003183MODULE_DESCRIPTION("TSPP platform device and char dev");
Joel Nider5556a852011-10-16 10:52:13 +02003184MODULE_LICENSE("GPL v2");