blob: 75b10208e100a633e51964c2a3a28cd4b5e12149 [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.
63 * Restrict it to half of this to save DMA memory.
64 */
65#define TSPP_SPS_DESCRIPTOR_COUNT (4 * 1024 - 1)
Joel Nider5556a852011-10-16 10:52:13 +020066#define TSPP_PACKET_LENGTH 188
67#define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH)
Hamad Kadmany567bed82012-11-29 14:15:57 +020068
69/* Max descriptor buffer size allowed by SPS */
70#define TSPP_MAX_BUFFER_SIZE (32 * 1024 - 1)
71
72/*
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +020073 * Returns whether to use DMA pool for TSPP output buffers.
74 * For buffers smaller than page size, using DMA pool
75 * provides better memory utilization as dma_alloc_coherent
76 * allocates minimum of page size.
77 */
78#define TSPP_USE_DMA_POOL(buff_size) ((buff_size) < PAGE_SIZE)
79
80/*
Hamad Kadmany567bed82012-11-29 14:15:57 +020081 * Max allowed TSPP buffers/descriptors.
82 * If SPS desc FIFO holds X descriptors, we can queue up to X-1 descriptors.
83 */
84#define TSPP_NUM_BUFFERS (TSPP_SPS_DESCRIPTOR_COUNT - 1)
Joel Nider5556a852011-10-16 10:52:13 +020085#define TSPP_TSIF_DEFAULT_TIME_LIMIT 60
86#define SPS_DESCRIPTOR_SIZE 8
87#define MIN_ACCEPTABLE_BUFFER_COUNT 2
Joel Nider5bd73f82011-12-14 16:53:30 +020088#define TSPP_DEBUG(msg...)
Joel Nider5556a852011-10-16 10:52:13 +020089
90/*
91 * TSIF register offsets
92 */
93#define TSIF_STS_CTL_OFF (0x0)
94#define TSIF_TIME_LIMIT_OFF (0x4)
95#define TSIF_CLK_REF_OFF (0x8)
96#define TSIF_LPBK_FLAGS_OFF (0xc)
97#define TSIF_LPBK_DATA_OFF (0x10)
98#define TSIF_TEST_CTL_OFF (0x14)
99#define TSIF_TEST_MODE_OFF (0x18)
100#define TSIF_TEST_RESET_OFF (0x1c)
101#define TSIF_TEST_EXPORT_OFF (0x20)
102#define TSIF_TEST_CURRENT_OFF (0x24)
103
104#define TSIF_DATA_PORT_OFF (0x100)
105
106/* bits for TSIF_STS_CTL register */
107#define TSIF_STS_CTL_EN_IRQ BIT(28)
108#define TSIF_STS_CTL_PACK_AVAIL BIT(27)
109#define TSIF_STS_CTL_1ST_PACKET BIT(26)
110#define TSIF_STS_CTL_OVERFLOW BIT(25)
111#define TSIF_STS_CTL_LOST_SYNC BIT(24)
112#define TSIF_STS_CTL_TIMEOUT BIT(23)
113#define TSIF_STS_CTL_INV_SYNC BIT(21)
114#define TSIF_STS_CTL_INV_NULL BIT(20)
115#define TSIF_STS_CTL_INV_ERROR BIT(19)
116#define TSIF_STS_CTL_INV_ENABLE BIT(18)
117#define TSIF_STS_CTL_INV_DATA BIT(17)
118#define TSIF_STS_CTL_INV_CLOCK BIT(16)
119#define TSIF_STS_CTL_SPARE BIT(15)
120#define TSIF_STS_CTL_EN_NULL BIT(11)
121#define TSIF_STS_CTL_EN_ERROR BIT(10)
122#define TSIF_STS_CTL_LAST_BIT BIT(9)
123#define TSIF_STS_CTL_EN_TIME_LIM BIT(8)
124#define TSIF_STS_CTL_EN_TCR BIT(7)
125#define TSIF_STS_CTL_TEST_MODE BIT(6)
Joel Nider5bd73f82011-12-14 16:53:30 +0200126#define TSIF_STS_CTL_MODE_2 BIT(5)
Joel Nider5556a852011-10-16 10:52:13 +0200127#define TSIF_STS_CTL_EN_DM BIT(4)
128#define TSIF_STS_CTL_STOP BIT(3)
129#define TSIF_STS_CTL_START BIT(0)
130
131/*
132 * TSPP register offsets
133 */
Liron Kuch229090d2012-10-30 17:47:50 +0200134#define TSPP_RST 0x00
Joel Nider5556a852011-10-16 10:52:13 +0200135#define TSPP_CLK_CONTROL 0x04
Liron Kuch229090d2012-10-30 17:47:50 +0200136#define TSPP_CONFIG 0x08
137#define TSPP_CONTROL 0x0C
Joel Nider5556a852011-10-16 10:52:13 +0200138#define TSPP_PS_DISABLE 0x10
Liron Kuch229090d2012-10-30 17:47:50 +0200139#define TSPP_MSG_IRQ_STATUS 0x14
Joel Nider5556a852011-10-16 10:52:13 +0200140#define TSPP_MSG_IRQ_MASK 0x18
141#define TSPP_IRQ_STATUS 0x1C
142#define TSPP_IRQ_MASK 0x20
143#define TSPP_IRQ_CLEAR 0x24
144#define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2))
Liron Kuch229090d2012-10-30 17:47:50 +0200145#define TSPP_STATUS 0x68
146#define TSPP_CURR_TSP_HEADER 0x6C
147#define TSPP_CURR_PID_FILTER 0x70
148#define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2))
149#define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2))
150#define TSPP_DATA_KEY_RESET 0x9C
Joel Nider5556a852011-10-16 10:52:13 +0200151#define TSPP_KEY_VALID 0xA0
152#define TSPP_KEY_ERROR 0xA4
153#define TSPP_TEST_CTRL 0xA8
Liron Kuch229090d2012-10-30 17:47:50 +0200154#define TSPP_VERSION 0xAC
Joel Nider5556a852011-10-16 10:52:13 +0200155#define TSPP_GENERICS 0xB0
Liron Kuch229090d2012-10-30 17:47:50 +0200156#define TSPP_NOP 0xB4
Joel Nider5556a852011-10-16 10:52:13 +0200157
158/*
159 * Register bit definitions
160 */
161/* TSPP_RST */
162#define TSPP_RST_RESET BIT(0)
163
164/* TSPP_CLK_CONTROL */
165#define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9)
166#define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8)
167#define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7)
168#define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6)
169#define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5)
170#define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4)
171#define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3)
172#define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2)
173#define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1)
174#define TSPP_CLK_CONTROL_SET_CLKON BIT(0)
175
176/* TSPP_CONFIG */
177#define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \
178((_b & 0xF) << 8))
179#define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF)
180#define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7)
181#define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6)
182#define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5)
183#define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4)
184#define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3)
185#define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2)
186#define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1)
187#define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0)
188
189/* TSPP_CONTROL */
190#define TSPP_CONTROL_PID_FILTER_LOCK BIT(5)
191#define TSPP_CONTROL_FORCE_KEY_CALC BIT(4)
192#define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3)
193#define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2)
194#define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1)
195#define TSPP_CONTROL_PERF_COUNT_INIT BIT(0)
196
197/* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */
198#define TSPP_MSG_TSPP_IRQ BIT(2)
199#define TSPP_MSG_TSIF_1_IRQ BIT(1)
200#define TSPP_MSG_TSIF_0_IRQ BIT(0)
201
202/* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */
Liron Kuch229090d2012-10-30 17:47:50 +0200203#define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19)
204#define TSPP_IRQ_STATUS_KEY_ERROR BIT(18)
Joel Nider5556a852011-10-16 10:52:13 +0200205#define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17)
206#define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16)
207#define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n))
208
209/* TSPP_PIPE_ERROR_STATUS */
Liron Kuch229090d2012-10-30 17:47:50 +0200210#define TSPP_PIPE_PES_SYNC_ERROR BIT(3)
211#define TSPP_PIPE_PS_LENGTH_ERROR BIT(2)
Joel Nider5556a852011-10-16 10:52:13 +0200212#define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1)
Liron Kuch229090d2012-10-30 17:47:50 +0200213#define TSPP_PIP_PS_LOST_START BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200214
215/* TSPP_STATUS */
Liron Kuch229090d2012-10-30 17:47:50 +0200216#define TSPP_STATUS_TSP_PKT_AVAIL BIT(10)
217#define TSPP_STATUS_TSIF1_DM_REQ BIT(6)
218#define TSPP_STATUS_TSIF0_DM_REQ BIT(2)
219#define TSPP_CURR_FILTER_TABLE BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200220
221/* TSPP_GENERICS */
Liron Kuch229090d2012-10-30 17:47:50 +0200222#define TSPP_GENERICS_CRYPTO_GEN BIT(12)
Joel Nider5556a852011-10-16 10:52:13 +0200223#define TSPP_GENERICS_MAX_CONS_PIPES BIT(7)
Liron Kuch229090d2012-10-30 17:47:50 +0200224#define TSPP_GENERICS_MAX_PIPES BIT(2)
225#define TSPP_GENERICS_TSIF_1_GEN BIT(1)
226#define TSPP_GENERICS_TSIF_0_GEN BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200227
228/*
229 * TSPP memory regions
230 */
231#define TSPP_PID_FILTER_TABLE0 0x800
232#define TSPP_PID_FILTER_TABLE1 0x880
233#define TSPP_PID_FILTER_TABLE2 0x900
234#define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */
235#define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */
236#define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */
237#define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2))
238#define TSPP_DATA_KEY 0xCD0
239
Joel Nider5556a852011-10-16 10:52:13 +0200240struct debugfs_entry {
241 const char *name;
242 mode_t mode;
243 int offset;
244};
245
246static const struct debugfs_entry debugfs_tsif_regs[] = {
247 {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF},
248 {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF},
249 {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF},
250 {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF},
251 {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF},
252 {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF},
253 {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF},
254 {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF},
255 {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF},
256 {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF},
257 {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF},
258};
259
260static const struct debugfs_entry debugfs_tspp_regs[] = {
261 {"rst", S_IRUGO | S_IWUSR, TSPP_RST},
262 {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL},
263 {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG},
264 {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL},
265 {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE},
266 {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS},
267 {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK},
268 {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS},
269 {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK},
270 {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR},
271 /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */
272 {"status", S_IRUGO | S_IWUSR, TSPP_STATUS},
273 {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER},
274 {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER},
275 /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */
276 /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */
277 {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET},
278 {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID},
279 {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR},
280 {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL},
281 {"version", S_IRUGO | S_IWUSR, TSPP_VERSION},
282 {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS},
283 {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0},
284 {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1},
285 {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2},
286 {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE},
287 {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT},
288 {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE},
289 {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY}
290};
291
Joel Nider5556a852011-10-16 10:52:13 +0200292struct tspp_pid_filter {
293 u32 filter; /* see FILTER_ macros */
294 u32 config; /* see FILTER_ macros */
295};
296
297/* tsp_info */
298#define FILTER_HEADER_ERROR_MASK BIT(7)
299#define FILTER_TRANS_END_DISABLE BIT(6)
300#define FILTER_DEC_ON_ERROR_EN BIT(5)
301#define FILTER_DECRYPT BIT(4)
302#define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT)
303#define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF)
304#define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \
305 (_p->config & ~0xF) | (_b & 0xF))
306#define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3)
307#define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \
308 (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30))
309#define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF)
310#define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \
311 (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13))
312#define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF)
313#define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \
314 (_p->filter & ~0x1FFF) | (_b & 0x1FFF))
315#define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3)
316#define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \
317 (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30))
318#define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7)
319#define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \
320 (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8))
321
322struct tspp_global_performance_regs {
323 u32 tsp_total;
324 u32 tsp_ignored;
325 u32 tsp_error;
326 u32 tsp_sync;
327};
328
329struct tspp_pipe_context_regs {
330 u16 pes_bytes_left;
331 u16 count;
332 u32 tsif_suffix;
333} __packed;
334#define CONTEXT_GET_STATE(_a) (_a & 0x3)
335#define CONTEXT_UNSPEC_LENGTH BIT(11)
336#define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF)
337
Hamad Kadmany567bed82012-11-29 14:15:57 +0200338#define MSEC_TO_JIFFIES(msec) ((msec) * HZ / 1000)
339
Joel Nider5556a852011-10-16 10:52:13 +0200340struct tspp_pipe_performance_regs {
341 u32 tsp_total;
342 u32 ps_duplicate_tsp;
343 u32 tsp_no_payload;
344 u32 tsp_broken_ps;
345 u32 ps_total_num;
346 u32 ps_continuity_error;
347 u32 ps_length_error;
348 u32 pes_sync_error;
349};
350
351struct tspp_tsif_device {
352 void __iomem *base;
353 u32 time_limit;
354 u32 ref_count;
Joel Nider5bd73f82011-12-14 16:53:30 +0200355 enum tspp_tsif_mode mode;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200356 int clock_inverse;
357 int data_inverse;
358 int sync_inverse;
359 int enable_inverse;
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200360 u32 tsif_irq;
Joel Nider5556a852011-10-16 10:52:13 +0200361
362 /* debugfs */
Joel Nider5556a852011-10-16 10:52:13 +0200363 struct dentry *dent_tsif;
364 struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)];
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200365 u32 stat_rx;
366 u32 stat_overflow;
367 u32 stat_lost_sync;
368 u32 stat_timeout;
Joel Nider5556a852011-10-16 10:52:13 +0200369};
370
371enum tspp_buf_state {
372 TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */
373 TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */
Joel Nider5bd73f82011-12-14 16:53:30 +0200374 TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */
375 TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */
Joel Nider5556a852011-10-16 10:52:13 +0200376};
377
378struct tspp_mem_buffer {
Joel Nider5bd73f82011-12-14 16:53:30 +0200379 struct tspp_mem_buffer *next;
380 struct sps_mem_buffer sps;
381 struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */
Joel Nider5556a852011-10-16 10:52:13 +0200382 enum tspp_buf_state state;
383 size_t filled; /* how much data this buffer is holding */
384 int read_index; /* where to start reading data from */
385};
386
387/* this represents each char device 'channel' */
388struct tspp_channel {
389 struct cdev cdev;
390 struct device *dd;
Joel Nider5bd73f82011-12-14 16:53:30 +0200391 struct tspp_device *pdev; /* can use container_of instead? */
Joel Nider5556a852011-10-16 10:52:13 +0200392 struct sps_pipe *pipe;
393 struct sps_connect config;
394 struct sps_register_event event;
Joel Nider5bd73f82011-12-14 16:53:30 +0200395 struct tspp_mem_buffer *data; /* list of buffers */
396 struct tspp_mem_buffer *read; /* first buffer ready to be read */
397 struct tspp_mem_buffer *waiting; /* first outstanding transfer */
398 struct tspp_mem_buffer *locked; /* buffer currently being read */
Joel Nider5556a852011-10-16 10:52:13 +0200399 wait_queue_head_t in_queue; /* set when data is received */
Joel Nider5bd73f82011-12-14 16:53:30 +0200400 u32 id; /* channel id (0-15) */
401 int used; /* is this channel in use? */
402 int key; /* which encryption key index is used */
403 u32 buffer_size; /* size of the sps transfer buffers */
404 u32 max_buffers; /* how many buffers should be allocated */
405 u32 buffer_count; /* how many buffers are actually allocated */
406 u32 filter_count; /* how many filters have been added to this channel */
407 u32 int_freq; /* generate interrupts every x descriptors */
Joel Nider5556a852011-10-16 10:52:13 +0200408 enum tspp_source src;
409 enum tspp_mode mode;
Joel Nider5bd73f82011-12-14 16:53:30 +0200410 tspp_notifier *notifier; /* used only with kernel api */
411 void *notify_data; /* data to be passed with the notifier */
Hamad Kadmany567bed82012-11-29 14:15:57 +0200412 u32 expiration_period_ms; /* notification on partially filled buffers */
413 struct timer_list expiration_timer;
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200414 struct dma_pool *dma_pool;
Liron Kuch229090d2012-10-30 17:47:50 +0200415 tspp_memfree *memfree; /* user defined memory free function */
416 void *user_info; /* user cookie passed to memory alloc/free function */
Joel Nider5556a852011-10-16 10:52:13 +0200417};
418
419struct tspp_pid_filter_table {
420 struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES];
421};
422
423struct tspp_key_entry {
424 u32 even_lsb;
425 u32 even_msb;
426 u32 odd_lsb;
427 u32 odd_msb;
428};
429
430struct tspp_key_table {
431 struct tspp_key_entry entry[TSPP_NUM_KEYS];
432};
433
Joel Nider5bd73f82011-12-14 16:53:30 +0200434/* this represents the actual hardware device */
435struct tspp_device {
436 struct list_head devlist; /* list of all devices */
437 struct platform_device *pdev;
438 void __iomem *base;
439 unsigned int tspp_irq;
440 unsigned int bam_irq;
441 u32 bam_handle;
442 struct sps_bam_props bam_props;
443 struct wake_lock wake_lock;
444 spinlock_t spinlock;
445 struct tasklet_struct tlet;
446 struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES];
447 /* clocks */
448 struct clk *tsif_pclk;
449 struct clk *tsif_ref_clk;
450 /* data */
451 struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES];
452 struct tspp_channel channels[TSPP_NUM_CHANNELS];
453 struct tspp_key_table *tspp_key_table;
454 struct tspp_global_performance_regs *tspp_global_performance;
455 struct tspp_pipe_context_regs *tspp_pipe_context;
456 struct tspp_pipe_performance_regs *tspp_pipe_performance;
457
458 struct dentry *dent;
459 struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)];
460};
461
462
Joel Nider5556a852011-10-16 10:52:13 +0200463static struct class *tspp_class;
464static int tspp_key_entry;
465static dev_t tspp_minor; /* next minor number to assign */
Joel Nider5bd73f82011-12-14 16:53:30 +0200466
467static LIST_HEAD(tspp_devices);
468
469/* forward declarations */
470static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *);
471static ssize_t tspp_open(struct inode *inode, struct file *filp);
472static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p);
473static ssize_t tspp_release(struct inode *inode, struct file *filp);
474static long tspp_ioctl(struct file *, unsigned int, unsigned long);
475
476/* file operations */
477static const struct file_operations tspp_fops = {
478 .owner = THIS_MODULE,
479 .read = tspp_read,
480 .open = tspp_open,
481 .poll = tspp_poll,
482 .release = tspp_release,
483 .unlocked_ioctl = tspp_ioctl,
484};
Joel Nider5556a852011-10-16 10:52:13 +0200485
486/*** IRQ ***/
Joel Nider5bd73f82011-12-14 16:53:30 +0200487static irqreturn_t tspp_isr(int irq, void *dev)
Joel Nider5556a852011-10-16 10:52:13 +0200488{
Joel Nider5bd73f82011-12-14 16:53:30 +0200489 struct tspp_device *device = dev;
Joel Nider5556a852011-10-16 10:52:13 +0200490 u32 status, mask;
491 u32 data;
492
493 status = readl_relaxed(device->base + TSPP_IRQ_STATUS);
494 mask = readl_relaxed(device->base + TSPP_IRQ_MASK);
495 status &= mask;
496
497 if (!status) {
498 dev_warn(&device->pdev->dev, "Spurious interrupt");
499 return IRQ_NONE;
500 }
501
502 /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */
503
504 if (status & TSPP_IRQ_STATUS_KEY_ERROR) {
505 /* read the key error info */
506 data = readl_relaxed(device->base + TSPP_KEY_ERROR);
507 dev_info(&device->pdev->dev, "key error 0x%x", data);
508 }
509 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) {
510 data = readl_relaxed(device->base + TSPP_KEY_VALID);
511 dev_info(&device->pdev->dev, "key invalidated: 0x%x", data);
512 }
513 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED)
514 dev_info(&device->pdev->dev, "key switched");
515
516 if (status & 0xffff)
Joel Nider5bd73f82011-12-14 16:53:30 +0200517 dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff);
Joel Nider5556a852011-10-16 10:52:13 +0200518
519 writel_relaxed(status, device->base + TSPP_IRQ_CLEAR);
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200520
521 /*
522 * Before returning IRQ_HANDLED to the generic interrupt handling
523 * framework need to make sure all operations including clearing of
524 * interrupt status registers in the hardware is performed.
525 * Thus a barrier after clearing the interrupt status register
526 * is required to guarantee that the interrupt status register has
527 * really been cleared by the time we return from this handler.
528 */
529 wmb();
530 return IRQ_HANDLED;
531}
532
533static irqreturn_t tsif_isr(int irq, void *dev)
534{
535 struct tspp_tsif_device *tsif_device = dev;
536 u32 sts_ctl = ioread32(tsif_device->base + TSIF_STS_CTL_OFF);
537
538 if (!(sts_ctl & (TSIF_STS_CTL_PACK_AVAIL |
539 TSIF_STS_CTL_OVERFLOW |
540 TSIF_STS_CTL_LOST_SYNC |
541 TSIF_STS_CTL_TIMEOUT)))
542 return IRQ_NONE;
543
544 if (sts_ctl & TSIF_STS_CTL_OVERFLOW)
545 tsif_device->stat_overflow++;
546
547 if (sts_ctl & TSIF_STS_CTL_LOST_SYNC)
548 tsif_device->stat_lost_sync++;
549
550 if (sts_ctl & TSIF_STS_CTL_TIMEOUT)
551 tsif_device->stat_timeout++;
552
553 iowrite32(sts_ctl, tsif_device->base + TSIF_STS_CTL_OFF);
554
555 /*
556 * Before returning IRQ_HANDLED to the generic interrupt handling
557 * framework need to make sure all operations including clearing of
558 * interrupt status registers in the hardware is performed.
559 * Thus a barrier after clearing the interrupt status register
560 * is required to guarantee that the interrupt status register has
561 * really been cleared by the time we return from this handler.
562 */
Joel Nider5556a852011-10-16 10:52:13 +0200563 wmb();
564 return IRQ_HANDLED;
565}
566
567/*** callbacks ***/
568static void tspp_sps_complete_cb(struct sps_event_notify *notify)
569{
Joel Nider5bd73f82011-12-14 16:53:30 +0200570 struct tspp_device *pdev = notify->user;
571 tasklet_schedule(&pdev->tlet);
Joel Nider5556a852011-10-16 10:52:13 +0200572}
573
Hamad Kadmany567bed82012-11-29 14:15:57 +0200574static void tspp_expiration_timer(unsigned long data)
575{
576 struct tspp_device *pdev = (struct tspp_device *)data;
577
578 if (pdev)
579 tasklet_schedule(&pdev->tlet);
580}
581
Joel Nider5556a852011-10-16 10:52:13 +0200582/*** tasklet ***/
583static void tspp_sps_complete_tlet(unsigned long data)
584{
585 int i;
586 int complete;
587 unsigned long flags;
588 struct sps_iovec iovec;
589 struct tspp_channel *channel;
590 struct tspp_device *device = (struct tspp_device *)data;
Joel Nider5556a852011-10-16 10:52:13 +0200591 spin_lock_irqsave(&device->spinlock, flags);
592
593 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
594 complete = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +0200595 channel = &device->channels[i];
Hamad Kadmany567bed82012-11-29 14:15:57 +0200596
Joel Nider5bd73f82011-12-14 16:53:30 +0200597 if (!channel->used || !channel->waiting)
598 continue;
Joel Nider5556a852011-10-16 10:52:13 +0200599
Hamad Kadmany567bed82012-11-29 14:15:57 +0200600 /* stop the expiration timer */
601 if (channel->expiration_period_ms)
602 del_timer(&channel->expiration_timer);
603
Joel Nider5556a852011-10-16 10:52:13 +0200604 /* get completions */
Joel Nider5bd73f82011-12-14 16:53:30 +0200605 while (channel->waiting->state == TSPP_BUF_STATE_WAITING) {
Joel Nider5556a852011-10-16 10:52:13 +0200606 if (sps_get_iovec(channel->pipe, &iovec) != 0) {
607 pr_err("tspp: Error in iovec on channel %i",
608 channel->id);
609 break;
610 }
611 if (iovec.size == 0)
612 break;
613
Joel Nider5bd73f82011-12-14 16:53:30 +0200614 if (iovec.addr != channel->waiting->sps.phys_base)
Joel Nider5556a852011-10-16 10:52:13 +0200615 pr_err("tspp: buffer mismatch 0x%08x",
Joel Nider5bd73f82011-12-14 16:53:30 +0200616 channel->waiting->sps.phys_base);
Joel Nider5556a852011-10-16 10:52:13 +0200617
618 complete = 1;
Joel Nider5bd73f82011-12-14 16:53:30 +0200619 channel->waiting->state = TSPP_BUF_STATE_DATA;
620 channel->waiting->filled = iovec.size;
621 channel->waiting->read_index = 0;
622
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200623 if (channel->src == TSPP_SOURCE_TSIF0)
624 device->tsif[0].stat_rx++;
625 else if (channel->src == TSPP_SOURCE_TSIF1)
626 device->tsif[1].stat_rx++;
627
Joel Nider5bd73f82011-12-14 16:53:30 +0200628 /* update the pointers */
629 channel->waiting = channel->waiting->next;
Joel Nider5556a852011-10-16 10:52:13 +0200630 }
631
Joel Nider5bd73f82011-12-14 16:53:30 +0200632 /* wake any waiting processes */
Joel Nider5556a852011-10-16 10:52:13 +0200633 if (complete) {
Joel Nider5556a852011-10-16 10:52:13 +0200634 wake_up_interruptible(&channel->in_queue);
Joel Nider5bd73f82011-12-14 16:53:30 +0200635
636 /* call notifiers */
637 if (channel->notifier)
638 channel->notifier(channel->id,
639 channel->notify_data);
Joel Nider5556a852011-10-16 10:52:13 +0200640 }
Hamad Kadmany567bed82012-11-29 14:15:57 +0200641
642 /* restart expiration timer */
643 if (channel->expiration_period_ms)
644 mod_timer(&channel->expiration_timer,
645 jiffies +
646 MSEC_TO_JIFFIES(
647 channel->expiration_period_ms));
Joel Nider5556a852011-10-16 10:52:13 +0200648 }
649
650 spin_unlock_irqrestore(&device->spinlock, flags);
651}
652
653/*** GPIO functions ***/
Liron Kuch65067fa2013-02-10 15:19:32 +0200654static int tspp_gpios_disable(const struct tspp_tsif_device *tsif_device,
655 const struct msm_gpio *table,
656 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200657{
658 int rc = 0;
659 int i;
660 const struct msm_gpio *g;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200661
Joel Nider5556a852011-10-16 10:52:13 +0200662 for (i = size-1; i >= 0; i--) {
663 int tmp;
664 g = table + i;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200665
Liron Kuch65067fa2013-02-10 15:19:32 +0200666 /* don't use sync GPIO when not working in mode 2 */
667 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
668 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
669 continue;
670
Liron Kuch8fa85b02013-01-01 18:29:47 +0200671 tmp = gpio_tlmm_config(GPIO_CFG(GPIO_PIN(g->gpio_cfg),
672 0, GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),
673 GPIO_CFG_DISABLE);
Joel Nider5556a852011-10-16 10:52:13 +0200674 if (tmp) {
Liron Kuch229090d2012-10-30 17:47:50 +0200675 pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200676 g->gpio_cfg, g->label ?: "?", rc);
677 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
678 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
679 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
680 GPIO_DRVSTR(g->gpio_cfg));
681 if (!rc)
682 rc = tmp;
683 }
684 }
685
686 return rc;
687}
688
Liron Kuch65067fa2013-02-10 15:19:32 +0200689static int tspp_gpios_enable(const struct tspp_tsif_device *tsif_device,
690 const struct msm_gpio *table,
691 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200692{
693 int rc;
Liron Kuch65067fa2013-02-10 15:19:32 +0200694 int i;
Joel Nider5556a852011-10-16 10:52:13 +0200695 const struct msm_gpio *g;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200696
Joel Nider5556a852011-10-16 10:52:13 +0200697 for (i = 0; i < size; i++) {
698 g = table + i;
Liron Kuch65067fa2013-02-10 15:19:32 +0200699
700 /* don't use sync GPIO when not working in mode 2 */
701 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
702 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
703 continue;
704
Joel Nider5556a852011-10-16 10:52:13 +0200705 rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
706 if (rc) {
Liron Kuch229090d2012-10-30 17:47:50 +0200707 pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200708 g->gpio_cfg, g->label ?: "?", rc);
709 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
710 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
711 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
712 GPIO_DRVSTR(g->gpio_cfg));
713 goto err;
714 }
715 }
716 return 0;
717err:
Liron Kuch65067fa2013-02-10 15:19:32 +0200718 tspp_gpios_disable(tsif_device, table, i);
Joel Nider5556a852011-10-16 10:52:13 +0200719
Joel Nider5556a852011-10-16 10:52:13 +0200720 return rc;
721}
722
Liron Kuch65067fa2013-02-10 15:19:32 +0200723
724static int tspp_config_gpios(struct tspp_device *device,
725 enum tspp_source source,
726 int enable)
Joel Nider5556a852011-10-16 10:52:13 +0200727{
Liron Kuch65067fa2013-02-10 15:19:32 +0200728 const struct msm_gpio *table;
729 struct msm_tspp_platform_data *pdata = device->pdev->dev.platform_data;
730 int num_gpios = (pdata->num_gpios / TSPP_TSIF_INSTANCES);
731 int i = 0;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200732
Liron Kuch65067fa2013-02-10 15:19:32 +0200733 if (num_gpios != TSPP_GPIOS_PER_TSIF) {
734 pr_err("tspp %s: unexpected number of GPIOs %d, expected %d\n",
735 __func__, num_gpios, TSPP_GPIOS_PER_TSIF);
736 return -EINVAL;
737 }
Joel Nider5556a852011-10-16 10:52:13 +0200738
Liron Kuch65067fa2013-02-10 15:19:32 +0200739 /*
740 * Note: this code assumes that the GPIO definitions in the
741 * pdata->gpios table are according to the TSIF instance number,
742 * i.e., that TSIF0 GPIOs are defined first, then TSIF1 GPIOs etc.
743 */
744 switch (source) {
745 case TSPP_SOURCE_TSIF0:
746 i = 0;
747 break;
748 case TSPP_SOURCE_TSIF1:
749 i = 1;
750 break;
751 default:
752 pr_err("tspp %s: invalid source\n", __func__);
753 return -EINVAL;
754 }
Liron Kuch8fa85b02013-01-01 18:29:47 +0200755
Liron Kuch65067fa2013-02-10 15:19:32 +0200756 table = pdata->gpios + (i * num_gpios);
757 if (enable)
758 return tspp_gpios_enable(&device->tsif[i], table, num_gpios);
759 else
760 return tspp_gpios_disable(&device->tsif[i], table, num_gpios);
Joel Nider5556a852011-10-16 10:52:13 +0200761}
762
Joel Nider5bd73f82011-12-14 16:53:30 +0200763/*** Clock functions ***/
764static int tspp_clock_start(struct tspp_device *device)
765{
Liron Kuchde8cbf92013-02-21 14:25:57 +0200766 if (device == NULL) {
767 pr_err("tspp: Can't start clocks, invalid device\n");
768 return -EINVAL;
769 }
770
Joel Nider5bd73f82011-12-14 16:53:30 +0200771 if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
772 pr_err("tspp: Can't start pclk");
773 return -EBUSY;
774 }
775
776 if (device->tsif_ref_clk &&
777 clk_prepare_enable(device->tsif_ref_clk) != 0) {
778 pr_err("tspp: Can't start ref clk");
779 clk_disable_unprepare(device->tsif_pclk);
780 return -EBUSY;
781 }
782
783 return 0;
784}
785
786static void tspp_clock_stop(struct tspp_device *device)
787{
Liron Kuchde8cbf92013-02-21 14:25:57 +0200788 if (device == NULL) {
789 pr_err("tspp: Can't stop clocks, invalid device\n");
790 return;
791 }
792
Joel Nider5bd73f82011-12-14 16:53:30 +0200793 if (device->tsif_pclk)
Liron Kuchde8cbf92013-02-21 14:25:57 +0200794 clk_disable_unprepare(device->tsif_pclk);
Joel Nider5bd73f82011-12-14 16:53:30 +0200795
796 if (device->tsif_ref_clk)
Liron Kuchde8cbf92013-02-21 14:25:57 +0200797 clk_disable_unprepare(device->tsif_ref_clk);
Joel Nider5bd73f82011-12-14 16:53:30 +0200798}
799
Joel Nider5556a852011-10-16 10:52:13 +0200800/*** TSIF functions ***/
801static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
802{
803 int start_hardware = 0;
804 u32 ctl;
805
806 if (tsif_device->ref_count == 0) {
807 start_hardware = 1;
808 } else if (tsif_device->ref_count > 0) {
809 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
810 if ((ctl & TSIF_STS_CTL_START) != 1) {
811 /* this hardware should already be running */
812 pr_warn("tspp: tsif hw not started but ref count > 0");
813 start_hardware = 1;
814 }
815 }
816
817 if (start_hardware) {
Joel Nider5bd73f82011-12-14 16:53:30 +0200818 ctl = TSIF_STS_CTL_EN_IRQ |
Joel Nider5556a852011-10-16 10:52:13 +0200819 TSIF_STS_CTL_EN_DM;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200820
821 if (tsif_device->clock_inverse)
822 ctl |= TSIF_STS_CTL_INV_CLOCK;
823
824 if (tsif_device->data_inverse)
825 ctl |= TSIF_STS_CTL_INV_DATA;
826
827 if (tsif_device->sync_inverse)
828 ctl |= TSIF_STS_CTL_INV_SYNC;
829
830 if (tsif_device->enable_inverse)
831 ctl |= TSIF_STS_CTL_INV_ENABLE;
832
Joel Nider5bd73f82011-12-14 16:53:30 +0200833 switch (tsif_device->mode) {
834 case TSPP_TSIF_MODE_LOOPBACK:
835 ctl |= TSIF_STS_CTL_EN_NULL |
836 TSIF_STS_CTL_EN_ERROR |
837 TSIF_STS_CTL_TEST_MODE;
838 break;
839 case TSPP_TSIF_MODE_1:
840 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
841 TSIF_STS_CTL_EN_TCR;
842 break;
843 case TSPP_TSIF_MODE_2:
844 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
845 TSIF_STS_CTL_EN_TCR |
846 TSIF_STS_CTL_MODE_2;
847 break;
848 default:
849 pr_warn("tspp: unknown tsif mode 0x%x",
850 tsif_device->mode);
Joel Nider5556a852011-10-16 10:52:13 +0200851 }
852 writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
853 writel_relaxed(tsif_device->time_limit,
854 tsif_device->base + TSIF_TIME_LIMIT_OFF);
855 wmb();
856 writel_relaxed(ctl | TSIF_STS_CTL_START,
857 tsif_device->base + TSIF_STS_CTL_OFF);
858 wmb();
Joel Nider5556a852011-10-16 10:52:13 +0200859 }
860
Joel Nider5bd73f82011-12-14 16:53:30 +0200861 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
Joel Nider5556a852011-10-16 10:52:13 +0200862 tsif_device->ref_count++;
863
Joel Nider5bd73f82011-12-14 16:53:30 +0200864 return (ctl & TSIF_STS_CTL_START) ? 0 : -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200865}
866
867static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
868{
869 if (tsif_device->ref_count == 0)
870 return;
871
872 tsif_device->ref_count--;
873
874 if (tsif_device->ref_count == 0) {
875 writel_relaxed(TSIF_STS_CTL_STOP,
876 tsif_device->base + TSIF_STS_CTL_OFF);
877 wmb();
878 }
879}
880
Joel Nider5bd73f82011-12-14 16:53:30 +0200881/*** local TSPP functions ***/
882static int tspp_channels_in_use(struct tspp_device *pdev)
883{
884 int i;
885 int count = 0;
886 for (i = 0; i < TSPP_NUM_CHANNELS; i++)
887 count += (pdev->channels[i].used ? 1 : 0);
888
889 return count;
890}
891
892static struct tspp_device *tspp_find_by_id(int id)
893{
894 struct tspp_device *dev;
895 list_for_each_entry(dev, &tspp_devices, devlist) {
896 if (dev->pdev->id == id)
897 return dev;
898 }
899 return NULL;
900}
901
Joel Nider5556a852011-10-16 10:52:13 +0200902static int tspp_get_key_entry(void)
903{
904 int i;
905 for (i = 0; i < TSPP_NUM_KEYS; i++) {
906 if (!(tspp_key_entry & (1 << i))) {
907 tspp_key_entry |= (1 << i);
908 return i;
909 }
910 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200911 return 1 < TSPP_NUM_KEYS;
Joel Nider5556a852011-10-16 10:52:13 +0200912}
913
914static void tspp_free_key_entry(int entry)
915{
916 if (entry > TSPP_NUM_KEYS) {
917 pr_err("tspp_free_key_entry: index out of bounds");
918 return;
919 }
920
921 tspp_key_entry &= ~(1 << entry);
922}
923
Joel Nider5bd73f82011-12-14 16:53:30 +0200924static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200925 u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user)
Joel Nider5556a852011-10-16 10:52:13 +0200926{
Joel Nider5bd73f82011-12-14 16:53:30 +0200927 if (size < TSPP_MIN_BUFFER_SIZE ||
928 size > TSPP_MAX_BUFFER_SIZE) {
929 pr_err("tspp: bad buffer size %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200930 return -ENOMEM;
931 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200932
933 if (alloc) {
934 TSPP_DEBUG("tspp using alloc function");
935 desc->virt_base = alloc(channel_id, size,
936 &desc->phys_base, user);
937 } else {
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200938 if (!dma_pool)
939 desc->virt_base = dma_alloc_coherent(NULL, size,
940 &desc->phys_base, GFP_KERNEL);
941 else
942 desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL,
943 &desc->phys_base);
944
Liron Kuch229090d2012-10-30 17:47:50 +0200945 if (desc->virt_base == 0) {
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200946 pr_err("tspp: dma buffer allocation failed %i\n", size);
Liron Kuch229090d2012-10-30 17:47:50 +0200947 return -ENOMEM;
948 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200949 }
950
951 desc->size = size;
952 return 0;
953}
954
955static int tspp_queue_buffer(struct tspp_channel *channel,
956 struct tspp_mem_buffer *buffer)
957{
958 int rc;
959 u32 flags = 0;
960
961 /* make sure the interrupt frequency is valid */
962 if (channel->int_freq < 1)
963 channel->int_freq = 1;
964
965 /* generate interrupt according to requested frequency */
966 if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
Hamad Kadmany567bed82012-11-29 14:15:57 +0200967 flags = SPS_IOVEC_FLAG_INT;
Joel Nider5bd73f82011-12-14 16:53:30 +0200968
969 /* start the transfer */
970 rc = sps_transfer_one(channel->pipe,
971 buffer->sps.phys_base,
972 buffer->sps.size,
973 channel->pdev,
974 flags);
975 if (rc < 0)
976 return rc;
977
978 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5556a852011-10-16 10:52:13 +0200979
980 return 0;
981}
982
983static int tspp_global_reset(struct tspp_device *pdev)
984{
985 u32 i, val;
986
987 /* stop all TSIFs */
988 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
989 pdev->tsif[i].ref_count = 1; /* allows stopping hw */
990 tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
991 pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200992 pdev->tsif[i].clock_inverse = 0;
993 pdev->tsif[i].data_inverse = 0;
994 pdev->tsif[i].sync_inverse = 0;
995 pdev->tsif[i].enable_inverse = 0;
Joel Nider5556a852011-10-16 10:52:13 +0200996 }
997 writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
998 wmb();
999
1000 /* BAM */
1001 if (sps_device_reset(pdev->bam_handle) != 0) {
1002 pr_err("tspp: error resetting bam");
Joel Nider5bd73f82011-12-14 16:53:30 +02001003 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001004 }
1005
1006 /* TSPP tables */
1007 for (i = 0; i < TSPP_FILTER_TABLES; i++)
Joel Nider5bd73f82011-12-14 16:53:30 +02001008 memset(pdev->filters[i],
Joel Nider5556a852011-10-16 10:52:13 +02001009 0, sizeof(struct tspp_pid_filter_table));
1010
1011 /* disable all filters */
1012 val = (2 << TSPP_NUM_CHANNELS) - 1;
1013 writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
1014
1015 /* TSPP registers */
1016 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1017 writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1018 pdev->base + TSPP_CONTROL);
1019 wmb();
Joel Nider5bd73f82011-12-14 16:53:30 +02001020 memset(pdev->tspp_global_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001021 sizeof(struct tspp_global_performance_regs));
Joel Nider5bd73f82011-12-14 16:53:30 +02001022 memset(pdev->tspp_pipe_context, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001023 sizeof(struct tspp_pipe_context_regs));
Joel Nider5bd73f82011-12-14 16:53:30 +02001024 memset(pdev->tspp_pipe_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001025 sizeof(struct tspp_pipe_performance_regs));
1026 wmb();
1027 writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1028 pdev->base + TSPP_CONTROL);
1029 wmb();
1030
1031 val = readl_relaxed(pdev->base + TSPP_CONFIG);
1032 val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
1033 TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
1034 TSPP_CONFIG_PS_CONT_ERR_MASK);
1035 TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
1036 writel_relaxed(val, pdev->base + TSPP_CONFIG);
Hamad Kadmany57f5ac82012-12-20 18:30:40 +02001037 writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK);
Joel Nider5556a852011-10-16 10:52:13 +02001038 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
1039 writel_relaxed(0, pdev->base + TSPP_RST);
1040 wmb();
1041
1042 tspp_key_entry = 0;
1043
1044 return 0;
1045}
1046
Joel Nider5bd73f82011-12-14 16:53:30 +02001047static int tspp_select_source(u32 dev, u32 channel_id,
1048 struct tspp_select_source *src)
1049{
1050 /* make sure the requested src id is in bounds */
1051 if (src->source > TSPP_SOURCE_MEM) {
1052 pr_err("tspp source out of bounds");
1053 return -EINVAL;
1054 }
1055
1056 /* open the stream */
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001057 tspp_open_stream(dev, channel_id, src);
Joel Nider5bd73f82011-12-14 16:53:30 +02001058
1059 return 0;
1060}
1061
1062static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
1063{
1064 struct tspp_device *pdev = channel->pdev;
1065
1066 writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
1067 writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
1068 return 0;
1069}
1070
1071static int tspp_set_system_keys(struct tspp_channel *channel,
1072 struct tspp_system_keys *keys)
1073{
1074 int i;
1075 struct tspp_device *pdev = channel->pdev;
1076
1077 for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
1078 writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
1079
1080 return 0;
1081}
1082
1083static int tspp_channel_init(struct tspp_channel *channel,
1084 struct tspp_device *pdev)
1085{
1086 channel->cdev.owner = THIS_MODULE;
1087 cdev_init(&channel->cdev, &tspp_fops);
1088 channel->pdev = pdev;
1089 channel->data = NULL;
1090 channel->read = NULL;
1091 channel->waiting = NULL;
1092 channel->locked = NULL;
1093 channel->id = MINOR(tspp_minor);
1094 channel->used = 0;
1095 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1096 channel->max_buffers = TSPP_NUM_BUFFERS;
1097 channel->buffer_count = 0;
1098 channel->filter_count = 0;
1099 channel->int_freq = 1;
Liron Kuch229090d2012-10-30 17:47:50 +02001100 channel->src = TSPP_SOURCE_NONE;
1101 channel->mode = TSPP_MODE_DISABLED;
Joel Nider5bd73f82011-12-14 16:53:30 +02001102 channel->notifier = NULL;
1103 channel->notify_data = NULL;
Hamad Kadmany567bed82012-11-29 14:15:57 +02001104 channel->expiration_period_ms = 0;
Liron Kuch229090d2012-10-30 17:47:50 +02001105 channel->memfree = NULL;
1106 channel->user_info = NULL;
Joel Nider5bd73f82011-12-14 16:53:30 +02001107 init_waitqueue_head(&channel->in_queue);
1108
1109 if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
1110 pr_err("tspp: cdev_add failed");
1111 return -EBUSY;
1112 }
1113
1114 channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
1115 channel, "tspp%02d", channel->id);
1116 if (IS_ERR(channel->dd)) {
1117 pr_err("tspp: device_create failed: %i",
1118 (int)PTR_ERR(channel->dd));
1119 cdev_del(&channel->cdev);
1120 return -EBUSY;
1121 }
1122
1123 return 0;
1124}
1125
1126static int tspp_set_buffer_size(struct tspp_channel *channel,
1127 struct tspp_buffer *buf)
1128{
Liron Kuch229090d2012-10-30 17:47:50 +02001129 if (channel->buffer_count > 0) {
1130 pr_err("tspp: cannot set buffer size - buffers already allocated\n");
1131 return -EPERM;
1132 }
1133
Joel Nider5bd73f82011-12-14 16:53:30 +02001134 if (buf->size < TSPP_MIN_BUFFER_SIZE)
1135 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1136 else if (buf->size > TSPP_MAX_BUFFER_SIZE)
1137 channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
1138 else
1139 channel->buffer_size = buf->size;
1140
1141 return 0;
1142}
1143
1144static void tspp_set_tsif_mode(struct tspp_channel *channel,
1145 enum tspp_tsif_mode mode)
1146{
1147 int index;
1148
1149 switch (channel->src) {
1150 case TSPP_SOURCE_TSIF0:
1151 index = 0;
1152 break;
1153 case TSPP_SOURCE_TSIF1:
1154 index = 1;
1155 break;
1156 default:
1157 pr_warn("tspp: can't set mode for non-tsif source %d",
1158 channel->src);
1159 return;
1160 }
1161 channel->pdev->tsif[index].mode = mode;
1162}
1163
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001164static void tspp_set_signal_inversion(struct tspp_channel *channel,
Liron Kuch229090d2012-10-30 17:47:50 +02001165 int clock_inverse, int data_inverse,
1166 int sync_inverse, int enable_inverse)
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001167{
1168 int index;
1169
1170 switch (channel->src) {
1171 case TSPP_SOURCE_TSIF0:
1172 index = 0;
1173 break;
1174 case TSPP_SOURCE_TSIF1:
1175 index = 1;
1176 break;
1177 default:
1178 return;
1179 }
1180 channel->pdev->tsif[index].clock_inverse = clock_inverse;
1181 channel->pdev->tsif[index].data_inverse = data_inverse;
1182 channel->pdev->tsif[index].sync_inverse = sync_inverse;
1183 channel->pdev->tsif[index].enable_inverse = enable_inverse;
1184}
1185
Liron Kuch229090d2012-10-30 17:47:50 +02001186static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode)
1187{
1188 u32 alignment;
1189
1190 switch (mode) {
1191 case TSPP_MODE_RAW:
1192 /* must be a multiple of 192 */
1193 alignment = (TSPP_PACKET_LENGTH + 4);
1194 if (size % alignment)
1195 return 0;
1196 return 1;
1197
1198 case TSPP_MODE_RAW_NO_SUFFIX:
1199 /* must be a multiple of 188 */
1200 alignment = TSPP_PACKET_LENGTH;
1201 if (size % alignment)
1202 return 0;
1203 return 1;
1204
1205 case TSPP_MODE_DISABLED:
1206 case TSPP_MODE_PES:
1207 default:
1208 /* no alignment requirement */
1209 return 1;
1210 }
1211
1212}
1213
1214static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode)
1215{
1216 u32 new_size;
1217 u32 alignment;
1218
1219 switch (mode) {
1220 case TSPP_MODE_RAW:
1221 /* must be a multiple of 192 */
1222 alignment = (TSPP_PACKET_LENGTH + 4);
1223 break;
1224
1225 case TSPP_MODE_RAW_NO_SUFFIX:
1226 /* must be a multiple of 188 */
1227 alignment = TSPP_PACKET_LENGTH;
1228 break;
1229
1230 case TSPP_MODE_DISABLED:
1231 case TSPP_MODE_PES:
1232 default:
1233 /* no alignment requirement - give the user what he asks for */
1234 alignment = 1;
1235 break;
1236 }
1237 /* align up */
1238 new_size = (((size + alignment - 1) / alignment) * alignment);
1239 return new_size;
1240}
1241
1242static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel)
1243{
1244 int i;
1245 struct tspp_mem_buffer *pbuf, *temp;
1246
1247 pbuf = channel->data;
1248 for (i = 0; i < channel->buffer_count; i++) {
1249 if (pbuf->desc.phys_base) {
1250 if (channel->memfree) {
1251 channel->memfree(channel_id,
1252 pbuf->desc.size,
1253 pbuf->desc.virt_base,
1254 pbuf->desc.phys_base,
1255 channel->user_info);
1256 } else {
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001257 if (!channel->dma_pool)
1258 dma_free_coherent(NULL,
1259 pbuf->desc.size,
1260 pbuf->desc.virt_base,
1261 pbuf->desc.phys_base);
1262 else
1263 dma_pool_free(channel->dma_pool,
1264 pbuf->desc.virt_base,
1265 pbuf->desc.phys_base);
Liron Kuch229090d2012-10-30 17:47:50 +02001266 }
1267 pbuf->desc.phys_base = 0;
1268 }
1269 pbuf->desc.virt_base = 0;
1270 pbuf->state = TSPP_BUF_STATE_EMPTY;
1271 temp = pbuf;
1272 pbuf = pbuf->next;
1273 kfree(temp);
1274 }
1275}
1276
Joel Nider5bd73f82011-12-14 16:53:30 +02001277/*** TSPP API functions ***/
Liron Kuch229090d2012-10-30 17:47:50 +02001278
1279/**
1280 * tspp_open_stream - open a TSPP stream for use.
1281 *
1282 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1283 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1284 * @source: stream source parameters.
1285 *
1286 * Return error status
1287 *
1288 */
1289int tspp_open_stream(u32 dev, u32 channel_id,
1290 struct tspp_select_source *source)
Joel Nider5556a852011-10-16 10:52:13 +02001291{
1292 u32 val;
1293 struct tspp_device *pdev;
Joel Nider5bd73f82011-12-14 16:53:30 +02001294 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001295
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001296 TSPP_DEBUG("tspp_open_stream %i %i %i %i",
1297 dev, channel_id, source->source, source->mode);
Liron Kuch229090d2012-10-30 17:47:50 +02001298
Joel Nider5bd73f82011-12-14 16:53:30 +02001299 if (dev >= TSPP_MAX_DEVICES) {
1300 pr_err("tspp: device id out of range");
1301 return -ENODEV;
1302 }
Joel Nider5556a852011-10-16 10:52:13 +02001303
Joel Nider5bd73f82011-12-14 16:53:30 +02001304 if (channel_id >= TSPP_NUM_CHANNELS) {
1305 pr_err("tspp: channel id out of range");
1306 return -ECHRNG;
1307 }
1308
1309 pdev = tspp_find_by_id(dev);
1310 if (!pdev) {
1311 pr_err("tspp_str: can't find device %i", dev);
1312 return -ENODEV;
1313 }
1314 channel = &pdev->channels[channel_id];
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001315 channel->src = source->source;
1316 tspp_set_tsif_mode(channel, source->mode);
1317 tspp_set_signal_inversion(channel, source->clk_inverse,
Liron Kuch229090d2012-10-30 17:47:50 +02001318 source->data_inverse, source->sync_inverse,
1319 source->enable_inverse);
Joel Nider5556a852011-10-16 10:52:13 +02001320
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001321 switch (source->source) {
Joel Nider5556a852011-10-16 10:52:13 +02001322 case TSPP_SOURCE_TSIF0:
Liron Kuch65067fa2013-02-10 15:19:32 +02001323 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1324 pr_err("tspp: error enabling tsif0 GPIOs\n");
1325 return -EBUSY;
1326 }
Joel Nider5556a852011-10-16 10:52:13 +02001327 /* make sure TSIF0 is running & enabled */
1328 if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
1329 pr_err("tspp: error starting tsif0");
Joel Nider5bd73f82011-12-14 16:53:30 +02001330 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001331 }
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 break;
1337 case TSPP_SOURCE_TSIF1:
Liron Kuch65067fa2013-02-10 15:19:32 +02001338 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1339 pr_err("tspp: error enabling tsif1 GPIOs\n");
1340 return -EBUSY;
1341 }
Joel Nider5556a852011-10-16 10:52:13 +02001342 /* make sure TSIF1 is running & enabled */
1343 if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
1344 pr_err("tspp: error starting tsif1");
Joel Nider5bd73f82011-12-14 16:53:30 +02001345 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001346 }
1347 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1348 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1349 pdev->base + TSPP_CONTROL);
1350 wmb();
1351 break;
1352 case TSPP_SOURCE_MEM:
1353 break;
1354 default:
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001355 pr_err("tspp: channel %i invalid source %i",
1356 channel->id, source->source);
Joel Nider5bd73f82011-12-14 16:53:30 +02001357 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001358 }
1359
Joel Nider5556a852011-10-16 10:52:13 +02001360 return 0;
1361}
1362EXPORT_SYMBOL(tspp_open_stream);
1363
Liron Kuch229090d2012-10-30 17:47:50 +02001364/**
1365 * tspp_close_stream - close a TSPP stream.
1366 *
1367 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1368 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1369 *
1370 * Return error status
1371 *
1372 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001373int tspp_close_stream(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001374{
1375 u32 val;
1376 struct tspp_device *pdev;
Joel Nider5bd73f82011-12-14 16:53:30 +02001377 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001378
Joel Nider5bd73f82011-12-14 16:53:30 +02001379 if (channel_id >= TSPP_NUM_CHANNELS) {
1380 pr_err("tspp: channel id out of range");
1381 return -ECHRNG;
1382 }
1383 pdev = tspp_find_by_id(dev);
1384 if (!pdev) {
1385 pr_err("tspp_cs: can't find device %i", dev);
1386 return -EBUSY;
1387 }
1388 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001389
1390 switch (channel->src) {
1391 case TSPP_SOURCE_TSIF0:
1392 tspp_stop_tsif(&pdev->tsif[0]);
Liron Kuch65067fa2013-02-10 15:19:32 +02001393 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1394 pr_err("tspp: error disabling tsif0 GPIOs\n");
1395
Joel Nider5556a852011-10-16 10:52:13 +02001396 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1397 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1398 pdev->base + TSPP_CONTROL);
1399 wmb();
1400 break;
1401 case TSPP_SOURCE_TSIF1:
1402 tspp_stop_tsif(&pdev->tsif[1]);
Liron Kuch65067fa2013-02-10 15:19:32 +02001403 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1404 pr_err("tspp: error disabling tsif0 GPIOs\n");
1405
Joel Nider5556a852011-10-16 10:52:13 +02001406 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1407 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1408 pdev->base + TSPP_CONTROL);
1409 break;
1410 case TSPP_SOURCE_MEM:
1411 break;
1412 case TSPP_SOURCE_NONE:
1413 break;
1414 }
1415
Joel Nider5bd73f82011-12-14 16:53:30 +02001416 channel->src = TSPP_SOURCE_NONE;
Joel Nider5556a852011-10-16 10:52:13 +02001417 return 0;
1418}
1419EXPORT_SYMBOL(tspp_close_stream);
1420
Liron Kuch229090d2012-10-30 17:47:50 +02001421/**
1422 * tspp_open_channel - open a TSPP channel.
1423 *
1424 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1425 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1426 *
1427 * Return error status
1428 *
1429 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001430int tspp_open_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001431{
1432 int rc = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001433 struct sps_connect *config;
1434 struct sps_register_event *event;
1435 struct tspp_channel *channel;
1436 struct tspp_device *pdev;
1437
1438 if (channel_id >= TSPP_NUM_CHANNELS) {
1439 pr_err("tspp: channel id out of range");
1440 return -ECHRNG;
1441 }
1442 pdev = tspp_find_by_id(dev);
1443 if (!pdev) {
1444 pr_err("tspp_oc: can't find device %i", dev);
1445 return -ENODEV;
1446 }
1447 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001448
1449 if (channel->used) {
1450 pr_err("tspp channel already in use");
Joel Nider5bd73f82011-12-14 16:53:30 +02001451 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001452 }
1453
Joel Nider5bd73f82011-12-14 16:53:30 +02001454 config = &channel->config;
1455 event = &channel->event;
1456
1457 /* start the clocks if needed */
Liron Kuch8fa85b02013-01-01 18:29:47 +02001458 if (tspp_channels_in_use(pdev) == 0) {
Liron Kuchde8cbf92013-02-21 14:25:57 +02001459 rc = tspp_clock_start(pdev);
1460 if (rc)
1461 return rc;
1462
Joel Nider5bd73f82011-12-14 16:53:30 +02001463 wake_lock(&pdev->wake_lock);
Liron Kuch8fa85b02013-01-01 18:29:47 +02001464 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001465
Joel Nider5556a852011-10-16 10:52:13 +02001466 /* mark it as used */
1467 channel->used = 1;
1468
1469 /* start the bam */
1470 channel->pipe = sps_alloc_endpoint();
1471 if (channel->pipe == 0) {
1472 pr_err("tspp: error allocating endpoint");
1473 rc = -ENOMEM;
1474 goto err_sps_alloc;
1475 }
1476
1477 /* get default configuration */
1478 sps_get_config(channel->pipe, config);
1479
Joel Nider5bd73f82011-12-14 16:53:30 +02001480 config->source = pdev->bam_handle;
Joel Nider5556a852011-10-16 10:52:13 +02001481 config->destination = SPS_DEV_HANDLE_MEM;
1482 config->mode = SPS_MODE_SRC;
Joel Nider5bd73f82011-12-14 16:53:30 +02001483 config->options =
1484 SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
1485 SPS_O_STREAMING | /* streaming mode */
1486 SPS_O_DESC_DONE | /* interrupt on end of descriptor */
Hamad Kadmany567bed82012-11-29 14:15:57 +02001487 SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */
1488 SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */
Joel Nider5556a852011-10-16 10:52:13 +02001489 config->src_pipe_index = channel->id;
1490 config->desc.size =
Hamad Kadmany567bed82012-11-29 14:15:57 +02001491 TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE;
Joel Nider5556a852011-10-16 10:52:13 +02001492 config->desc.base = dma_alloc_coherent(NULL,
1493 config->desc.size,
1494 &config->desc.phys_base,
1495 GFP_KERNEL);
1496 if (config->desc.base == 0) {
1497 pr_err("tspp: error allocating sps descriptors");
1498 rc = -ENOMEM;
1499 goto err_desc_alloc;
1500 }
1501
1502 memset(config->desc.base, 0, config->desc.size);
1503
1504 rc = sps_connect(channel->pipe, config);
1505 if (rc) {
1506 pr_err("tspp: error connecting bam");
1507 goto err_connect;
1508 }
1509
1510 event->mode = SPS_TRIGGER_CALLBACK;
Joel Nider5bd73f82011-12-14 16:53:30 +02001511 event->options = SPS_O_DESC_DONE;
Joel Nider5556a852011-10-16 10:52:13 +02001512 event->callback = tspp_sps_complete_cb;
1513 event->xfer_done = NULL;
Joel Nider5bd73f82011-12-14 16:53:30 +02001514 event->user = pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001515
1516 rc = sps_register_event(channel->pipe, event);
1517 if (rc) {
1518 pr_err("tspp: error registering event");
1519 goto err_event;
1520 }
1521
Hamad Kadmany567bed82012-11-29 14:15:57 +02001522 init_timer(&channel->expiration_timer);
1523 channel->expiration_timer.function = tspp_expiration_timer;
1524 channel->expiration_timer.data = (unsigned long)pdev;
1525 channel->expiration_timer.expires = 0xffffffffL;
1526
Joel Nider5bd73f82011-12-14 16:53:30 +02001527 rc = pm_runtime_get(&pdev->pdev->dev);
Joel Nider5556a852011-10-16 10:52:13 +02001528 if (rc < 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001529 dev_err(&pdev->pdev->dev,
Joel Nider5556a852011-10-16 10:52:13 +02001530 "Runtime PM: Unable to wake up tspp device, rc = %d",
1531 rc);
1532 }
Joel Nider5556a852011-10-16 10:52:13 +02001533 return 0;
1534
1535err_event:
1536 sps_disconnect(channel->pipe);
1537err_connect:
1538 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1539 config->desc.phys_base);
1540err_desc_alloc:
1541 sps_free_endpoint(channel->pipe);
1542err_sps_alloc:
1543 return rc;
1544}
1545EXPORT_SYMBOL(tspp_open_channel);
1546
Liron Kuch229090d2012-10-30 17:47:50 +02001547/**
1548 * tspp_close_channel - close a TSPP channel.
1549 *
1550 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1551 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1552 *
1553 * Return error status
1554 *
1555 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001556int tspp_close_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001557{
1558 int i;
1559 int id;
1560 u32 val;
Joel Nider5556a852011-10-16 10:52:13 +02001561
Joel Nider5bd73f82011-12-14 16:53:30 +02001562 struct sps_connect *config;
1563 struct tspp_device *pdev;
1564 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02001565
1566 if (channel_id >= TSPP_NUM_CHANNELS) {
1567 pr_err("tspp: channel id out of range");
1568 return -ECHRNG;
1569 }
1570 pdev = tspp_find_by_id(dev);
1571 if (!pdev) {
1572 pr_err("tspp_close: can't find device %i", dev);
1573 return -ENODEV;
1574 }
1575 channel = &pdev->channels[channel_id];
1576
1577 /* if the channel is not used, we are done */
1578 if (!channel->used)
1579 return 0;
1580
Hamad Kadmany567bed82012-11-29 14:15:57 +02001581 if (channel->expiration_period_ms)
1582 del_timer(&channel->expiration_timer);
1583
Joel Nider5bd73f82011-12-14 16:53:30 +02001584 channel->notifier = NULL;
1585 channel->notify_data = NULL;
Hamad Kadmany567bed82012-11-29 14:15:57 +02001586 channel->expiration_period_ms = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001587
1588 config = &channel->config;
1589 pdev = channel->pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001590
1591 /* disable pipe (channel) */
1592 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1593 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1594 wmb();
1595
1596 /* unregister all filters for this channel */
1597 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1598 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001599 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001600 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1601 if (id == channel->id) {
1602 if (FILTER_HAS_ENCRYPTION(tspp_filter))
1603 tspp_free_key_entry(
1604 FILTER_GET_KEY_NUMBER(tspp_filter));
1605 tspp_filter->config = 0;
1606 tspp_filter->filter = 0;
1607 }
1608 }
1609 channel->filter_count = 0;
1610
1611 /* stop the stream */
Joel Nider5bd73f82011-12-14 16:53:30 +02001612 tspp_close_stream(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02001613
1614 /* disconnect the bam */
1615 if (sps_disconnect(channel->pipe) != 0)
1616 pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
1617
1618 /* destroy the buffers */
1619 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1620 config->desc.phys_base);
1621
Liron Kuch229090d2012-10-30 17:47:50 +02001622 tspp_destroy_buffers(channel_id, channel);
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001623 if (channel->dma_pool) {
1624 dma_pool_destroy(channel->dma_pool);
1625 channel->dma_pool = NULL;
1626 }
Liron Kuch229090d2012-10-30 17:47:50 +02001627
1628 channel->src = TSPP_SOURCE_NONE;
1629 channel->mode = TSPP_MODE_DISABLED;
1630 channel->memfree = NULL;
1631 channel->user_info = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001632 channel->buffer_count = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001633 channel->data = NULL;
1634 channel->read = NULL;
1635 channel->waiting = NULL;
1636 channel->locked = NULL;
1637 channel->used = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001638
Liron Kuch8fa85b02013-01-01 18:29:47 +02001639 if (tspp_channels_in_use(pdev) == 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001640 wake_unlock(&pdev->wake_lock);
Liron Kuch8fa85b02013-01-01 18:29:47 +02001641 tspp_clock_stop(pdev);
1642 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001643
Liron Kuchde8cbf92013-02-21 14:25:57 +02001644 pm_runtime_put(&pdev->pdev->dev);
1645
Joel Nider5556a852011-10-16 10:52:13 +02001646 return 0;
1647}
1648EXPORT_SYMBOL(tspp_close_channel);
1649
Liron Kuch229090d2012-10-30 17:47:50 +02001650/**
Hamad Kadmany586fb392013-01-31 14:49:20 +02001651 * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter.
1652 *
1653 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1654 * @source: The TSIF source from which the counter should be read
1655 * @tcr_counter: the value of TCR counter
1656 *
1657 * Return error status
1658 *
1659 * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz.
1660 * If source is neither TSIF 0 or TSIF1 0 is returned.
1661 */
1662int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter)
1663{
1664 struct tspp_device *pdev;
1665 struct tspp_tsif_device *tsif_device;
1666
1667 if (!tcr_counter)
1668 return -EINVAL;
1669
1670 pdev = tspp_find_by_id(dev);
1671 if (!pdev) {
1672 pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev);
1673 return -ENODEV;
1674 }
1675
1676 switch (source) {
1677 case TSPP_SOURCE_TSIF0:
1678 tsif_device = &pdev->tsif[0];
1679 break;
1680
1681 case TSPP_SOURCE_TSIF1:
1682 tsif_device = &pdev->tsif[1];
1683 break;
1684
1685 default:
1686 tsif_device = NULL;
1687 break;
1688 }
1689
1690 if (tsif_device && tsif_device->ref_count)
1691 *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF);
1692 else
1693 *tcr_counter = 0;
1694
1695 return 0;
1696}
1697EXPORT_SYMBOL(tspp_get_ref_clk_counter);
1698
1699/**
Liron Kuch229090d2012-10-30 17:47:50 +02001700 * tspp_add_filter - add a TSPP filter to a channel.
1701 *
1702 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1703 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1704 * @filter: TSPP filter parameters
1705 *
1706 * Return error status
1707 *
1708 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001709int tspp_add_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001710 struct tspp_filter *filter)
1711{
Liron Kuch229090d2012-10-30 17:47:50 +02001712 int i, rc;
Joel Nider5556a852011-10-16 10:52:13 +02001713 int other_channel;
1714 int entry;
1715 u32 val, pid, enabled;
Joel Nider5bd73f82011-12-14 16:53:30 +02001716 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001717 struct tspp_pid_filter p;
Joel Nider5bd73f82011-12-14 16:53:30 +02001718 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001719
Joel Nider5bd73f82011-12-14 16:53:30 +02001720 TSPP_DEBUG("tspp: add filter");
1721 if (channel_id >= TSPP_NUM_CHANNELS) {
1722 pr_err("tspp: channel id out of range");
1723 return -ECHRNG;
1724 }
1725 pdev = tspp_find_by_id(dev);
1726 if (!pdev) {
1727 pr_err("tspp_add: can't find device %i", dev);
1728 return -ENODEV;
1729 }
1730
1731 channel = &pdev->channels[channel_id];
1732
Joel Nider5556a852011-10-16 10:52:13 +02001733 if (filter->source > TSPP_SOURCE_MEM) {
1734 pr_err("tspp invalid source");
Joel Nider5bd73f82011-12-14 16:53:30 +02001735 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001736 }
1737
1738 if (filter->priority >= TSPP_NUM_PRIORITIES) {
1739 pr_err("tspp invalid source");
Joel Nider5bd73f82011-12-14 16:53:30 +02001740 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001741 }
1742
Liron Kuch229090d2012-10-30 17:47:50 +02001743 channel->mode = filter->mode;
1744 /*
1745 * if buffers are already allocated, verify they fulfil
1746 * the alignment requirements.
1747 */
1748 if ((channel->buffer_count > 0) &&
1749 (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode)))
1750 pr_warn("tspp: buffers allocated with incorrect alignment\n");
Joel Nider5556a852011-10-16 10:52:13 +02001751
1752 if (filter->mode == TSPP_MODE_PES) {
1753 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1754 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001755 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001756 pid = FILTER_GET_PIPE_PID((tspp_filter));
1757 enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
1758 if (enabled && (pid == filter->pid)) {
1759 other_channel =
1760 FILTER_GET_PIPE_NUMBER0(tspp_filter);
1761 pr_err("tspp: pid 0x%x already in use by channel %i",
1762 filter->pid, other_channel);
Joel Nider5bd73f82011-12-14 16:53:30 +02001763 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001764 }
1765 }
1766 }
1767
1768 /* make sure this priority is not already in use */
1769 enabled = FILTER_GET_PIPE_PROCESS0(
Joel Nider5bd73f82011-12-14 16:53:30 +02001770 (&(pdev->filters[channel->src]->filter[filter->priority])));
Joel Nider5556a852011-10-16 10:52:13 +02001771 if (enabled) {
1772 pr_err("tspp: filter priority %i source %i is already enabled\n",
1773 filter->priority, channel->src);
Joel Nider5bd73f82011-12-14 16:53:30 +02001774 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001775 }
1776
1777 if (channel->mode == TSPP_MODE_PES) {
1778 /* if we are already processing in PES mode, disable pipe
1779 (channel) and filter to be updated */
1780 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1781 writel_relaxed(val | (1 << channel->id),
1782 pdev->base + TSPP_PS_DISABLE);
1783 wmb();
1784 }
1785
1786 /* update entry */
1787 p.filter = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001788 p.config = FILTER_TRANS_END_DISABLE;
Joel Nider5556a852011-10-16 10:52:13 +02001789 FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
1790 FILTER_SET_PIPE_PID((&p), filter->pid);
1791 FILTER_SET_PID_MASK((&p), filter->mask);
1792 FILTER_SET_PIPE_NUMBER0((&p), channel->id);
1793 FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
1794 if (filter->decrypt) {
1795 entry = tspp_get_key_entry();
1796 if (entry == -1) {
1797 pr_err("tspp: no more keys available!");
1798 } else {
1799 p.config |= FILTER_DECRYPT;
1800 FILTER_SET_KEY_NUMBER((&p), entry);
1801 }
1802 }
Joel Nider5556a852011-10-16 10:52:13 +02001803
Joel Nider5bd73f82011-12-14 16:53:30 +02001804 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001805 filter[filter->priority].config = p.config;
Joel Nider5bd73f82011-12-14 16:53:30 +02001806 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001807 filter[filter->priority].filter = p.filter;
1808
Liron Kuch229090d2012-10-30 17:47:50 +02001809 /*
1810 * allocate buffers if needed (i.e. if user did has not already called
1811 * tspp_allocate_buffers() explicitly).
1812 */
1813 if (channel->buffer_count == 0) {
1814 channel->buffer_size =
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001815 tspp_align_buffer_size_by_mode(channel->buffer_size,
Liron Kuch229090d2012-10-30 17:47:50 +02001816 channel->mode);
1817 rc = tspp_allocate_buffers(dev, channel->id,
1818 channel->max_buffers,
1819 channel->buffer_size,
1820 channel->int_freq, NULL, NULL, NULL);
1821 if (rc != 0) {
1822 pr_err("tspp: tspp_allocate_buffers failed\n");
1823 return rc;
1824 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001825 }
1826
Joel Nider5556a852011-10-16 10:52:13 +02001827 /* reenable pipe */
1828 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1829 writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
1830 wmb();
1831 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1832
Joel Nider5556a852011-10-16 10:52:13 +02001833 channel->filter_count++;
1834
1835 return 0;
1836}
1837EXPORT_SYMBOL(tspp_add_filter);
1838
Liron Kuch229090d2012-10-30 17:47:50 +02001839/**
1840 * tspp_remove_filter - remove a TSPP filter from a channel.
1841 *
1842 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1843 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1844 * @filter: TSPP filter parameters
1845 *
1846 * Return error status
1847 *
1848 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001849int tspp_remove_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001850 struct tspp_filter *filter)
1851{
1852 int entry;
1853 u32 val;
Joel Nider5bd73f82011-12-14 16:53:30 +02001854 struct tspp_device *pdev;
1855 int src;
1856 struct tspp_pid_filter *tspp_filter;
1857 struct tspp_channel *channel;
1858
1859 if (channel_id >= TSPP_NUM_CHANNELS) {
1860 pr_err("tspp: channel id out of range");
1861 return -ECHRNG;
1862 }
1863 pdev = tspp_find_by_id(dev);
1864 if (!pdev) {
1865 pr_err("tspp_remove: can't find device %i", dev);
1866 return -ENODEV;
1867 }
1868 channel = &pdev->channels[channel_id];
1869
1870 src = channel->src;
1871 tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
Joel Nider5556a852011-10-16 10:52:13 +02001872
1873 /* disable pipe (channel) */
1874 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1875 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1876 wmb();
1877
1878 /* update data keys */
1879 if (tspp_filter->config & FILTER_DECRYPT) {
1880 entry = FILTER_GET_KEY_NUMBER(tspp_filter);
1881 tspp_free_key_entry(entry);
1882 }
1883
1884 /* update pid table */
1885 tspp_filter->config = 0;
1886 tspp_filter->filter = 0;
1887
1888 channel->filter_count--;
1889
1890 /* reenable pipe */
1891 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1892 writel_relaxed(val & ~(1 << channel->id),
1893 pdev->base + TSPP_PS_DISABLE);
1894 wmb();
1895 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1896
1897 return 0;
1898}
1899EXPORT_SYMBOL(tspp_remove_filter);
1900
Liron Kuch229090d2012-10-30 17:47:50 +02001901/**
1902 * tspp_set_key - set TSPP key in key table.
1903 *
1904 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1905 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1906 * @key: TSPP key parameters
1907 *
1908 * Return error status
1909 *
1910 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001911int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
Joel Nider5556a852011-10-16 10:52:13 +02001912{
1913 int i;
1914 int id;
1915 int key_index;
1916 int data;
Joel Nider5bd73f82011-12-14 16:53:30 +02001917 struct tspp_channel *channel;
1918 struct tspp_device *pdev;
1919
1920 if (channel_id >= TSPP_NUM_CHANNELS) {
1921 pr_err("tspp: channel id out of range");
1922 return -ECHRNG;
1923 }
1924 pdev = tspp_find_by_id(dev);
1925 if (!pdev) {
1926 pr_err("tspp_set: can't find device %i", dev);
1927 return -ENODEV;
1928 }
1929 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001930
1931 /* read the key index used by this channel */
1932 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1933 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001934 &(pdev->filters[channel->src]->filter[i]);
Joel Nider5556a852011-10-16 10:52:13 +02001935 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1936 if (id == channel->id) {
1937 if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
1938 key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
1939 break;
1940 }
1941 }
1942 }
1943 if (i == TSPP_NUM_PRIORITIES) {
1944 pr_err("tspp: no encryption on this channel");
Joel Nider5bd73f82011-12-14 16:53:30 +02001945 return -ENOKEY;
Joel Nider5556a852011-10-16 10:52:13 +02001946 }
1947
1948 if (key->parity == TSPP_KEY_PARITY_EVEN) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001949 pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
1950 pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001951 } else {
Joel Nider5bd73f82011-12-14 16:53:30 +02001952 pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
1953 pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001954 }
1955 data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
1956
1957 return 0;
1958}
1959EXPORT_SYMBOL(tspp_set_key);
1960
Liron Kuch229090d2012-10-30 17:47:50 +02001961/**
1962 * tspp_register_notification - register TSPP channel notification function.
1963 *
1964 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1965 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1966 * @pNotify: notification function
1967 * @userdata: user data to pass to notification function
1968 * @timer_ms: notification for partially filled buffers
1969 *
1970 * Return error status
1971 *
1972 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001973int tspp_register_notification(u32 dev, u32 channel_id,
1974 tspp_notifier *pNotify, void *userdata, u32 timer_ms)
Joel Nider5556a852011-10-16 10:52:13 +02001975{
Joel Nider5bd73f82011-12-14 16:53:30 +02001976 struct tspp_channel *channel;
1977 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001978
Joel Nider5bd73f82011-12-14 16:53:30 +02001979 if (channel_id >= TSPP_NUM_CHANNELS) {
1980 pr_err("tspp: channel id out of range");
1981 return -ECHRNG;
1982 }
1983 pdev = tspp_find_by_id(dev);
1984 if (!pdev) {
1985 pr_err("tspp_reg: can't find device %i", dev);
1986 return -ENODEV;
1987 }
1988 channel = &pdev->channels[channel_id];
1989 channel->notifier = pNotify;
1990 channel->notify_data = userdata;
Hamad Kadmany567bed82012-11-29 14:15:57 +02001991 channel->expiration_period_ms = timer_ms;
1992
Joel Nider5556a852011-10-16 10:52:13 +02001993 return 0;
1994}
Joel Nider5bd73f82011-12-14 16:53:30 +02001995EXPORT_SYMBOL(tspp_register_notification);
Joel Nider5556a852011-10-16 10:52:13 +02001996
Liron Kuch229090d2012-10-30 17:47:50 +02001997/**
1998 * tspp_unregister_notification - unregister TSPP channel notification function.
1999 *
2000 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2001 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2002 *
2003 * Return error status
2004 *
2005 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002006int tspp_unregister_notification(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002007{
Joel Nider5bd73f82011-12-14 16:53:30 +02002008 struct tspp_channel *channel;
2009 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002010
Joel Nider5bd73f82011-12-14 16:53:30 +02002011 if (channel_id >= TSPP_NUM_CHANNELS) {
2012 pr_err("tspp: channel id out of range");
2013 return -ECHRNG;
2014 }
2015 pdev = tspp_find_by_id(dev);
2016 if (!pdev) {
2017 pr_err("tspp_unreg: can't find device %i", dev);
2018 return -ENODEV;
2019 }
2020 channel = &pdev->channels[channel_id];
2021 channel->notifier = NULL;
2022 channel->notify_data = 0;
Joel Nider5556a852011-10-16 10:52:13 +02002023 return 0;
2024}
Joel Nider5bd73f82011-12-14 16:53:30 +02002025EXPORT_SYMBOL(tspp_unregister_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002026
Liron Kuch229090d2012-10-30 17:47:50 +02002027/**
2028 * tspp_get_buffer - get TSPP data buffer.
2029 *
2030 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2031 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2032 *
2033 * Return error status
2034 *
2035 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002036const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002037{
Joel Nider5bd73f82011-12-14 16:53:30 +02002038 struct tspp_mem_buffer *buffer;
2039 struct tspp_channel *channel;
2040 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002041
Joel Nider5bd73f82011-12-14 16:53:30 +02002042 if (channel_id >= TSPP_NUM_CHANNELS) {
2043 pr_err("tspp: channel id out of range");
2044 return NULL;
2045 }
2046 pdev = tspp_find_by_id(dev);
2047 if (!pdev) {
2048 pr_err("tspp_get: can't find device %i", dev);
2049 return NULL;
2050 }
2051 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02002052
Joel Nider5bd73f82011-12-14 16:53:30 +02002053 if (!channel->read) {
2054 pr_warn("tspp: no buffer to get on channel %i!",
2055 channel->id);
2056 return NULL;
2057 }
2058
2059 buffer = channel->read;
2060 /* see if we have any buffers ready to read */
2061 if (buffer->state != TSPP_BUF_STATE_DATA)
2062 return 0;
2063
2064 if (buffer->state == TSPP_BUF_STATE_DATA) {
2065 /* mark the buffer as busy */
2066 buffer->state = TSPP_BUF_STATE_LOCKED;
2067
2068 /* increment the pointer along the list */
2069 channel->read = channel->read->next;
2070 }
2071
2072 return &buffer->desc;
2073}
2074EXPORT_SYMBOL(tspp_get_buffer);
2075
Liron Kuch229090d2012-10-30 17:47:50 +02002076/**
2077 * tspp_release_buffer - release TSPP data buffer back to TSPP.
2078 *
2079 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2080 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2081 * @descriptor_id: buffer descriptor ID
2082 *
2083 * Return error status
2084 *
2085 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002086int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
2087{
2088 int i, found = 0;
2089 struct tspp_mem_buffer *buffer;
2090 struct tspp_channel *channel;
2091 struct tspp_device *pdev;
2092
2093 if (channel_id >= TSPP_NUM_CHANNELS) {
2094 pr_err("tspp: channel id out of range");
2095 return -ECHRNG;
2096 }
2097 pdev = tspp_find_by_id(dev);
2098 if (!pdev) {
2099 pr_err("tspp: can't find device %i", dev);
2100 return -ENODEV;
2101 }
2102 channel = &pdev->channels[channel_id];
2103
2104 if (descriptor_id > channel->buffer_count)
2105 pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
2106
2107 /* find the correct descriptor */
2108 buffer = channel->locked;
2109 for (i = 0; i < channel->buffer_count; i++) {
2110 if (buffer->desc.id == descriptor_id) {
2111 found = 1;
2112 break;
2113 }
2114 buffer = buffer->next;
2115 }
2116 channel->locked = channel->locked->next;
2117
2118 if (!found) {
2119 pr_err("tspp: cant find desc %i", descriptor_id);
2120 return -EINVAL;
2121 }
2122
2123 /* make sure the buffer is in the expected state */
2124 if (buffer->state != TSPP_BUF_STATE_LOCKED) {
2125 pr_err("tspp: buffer %i not locked", descriptor_id);
2126 return -EINVAL;
2127 }
2128 /* unlock the buffer and requeue it */
2129 buffer->state = TSPP_BUF_STATE_WAITING;
2130
2131 if (tspp_queue_buffer(channel, buffer))
2132 pr_warn("tspp: can't requeue buffer");
Joel Nider5556a852011-10-16 10:52:13 +02002133 return 0;
2134}
Joel Nider5bd73f82011-12-14 16:53:30 +02002135EXPORT_SYMBOL(tspp_release_buffer);
2136
Liron Kuch229090d2012-10-30 17:47:50 +02002137/**
2138 * tspp_allocate_buffers - allocate TSPP data buffers.
2139 *
2140 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2141 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2142 * @count: number of buffers to allocate
2143 * @size: size of each buffer to allocate
2144 * @int_freq: interrupt frequency
2145 * @alloc: user defined memory allocator function. Pass NULL for default.
2146 * @memfree: user defined memory free function. Pass NULL for default.
2147 * @user: user data to pass to the memory allocator/free function
2148 *
2149 * Return error status
2150 *
2151 * The user can optionally call this function explicitly to allocate the TSPP
2152 * data buffers. Alternatively, if the user did not call this function, it
2153 * is called implicitly by tspp_add_filter().
2154 */
2155int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size,
2156 u32 int_freq, tspp_allocator *alloc,
2157 tspp_memfree *memfree, void *user)
Joel Nider5bd73f82011-12-14 16:53:30 +02002158{
2159 struct tspp_channel *channel;
2160 struct tspp_device *pdev;
2161 struct tspp_mem_buffer *last = NULL;
2162
2163 TSPP_DEBUG("tspp_allocate_buffers");
2164
2165 if (channel_id >= TSPP_NUM_CHANNELS) {
Liron Kuch229090d2012-10-30 17:47:50 +02002166 pr_err("%s: channel id out of range", __func__);
Joel Nider5bd73f82011-12-14 16:53:30 +02002167 return -ECHRNG;
2168 }
Liron Kuch229090d2012-10-30 17:47:50 +02002169
Joel Nider5bd73f82011-12-14 16:53:30 +02002170 pdev = tspp_find_by_id(dev);
2171 if (!pdev) {
Liron Kuch229090d2012-10-30 17:47:50 +02002172 pr_err("%s: can't find device %i", __func__, dev);
Joel Nider5bd73f82011-12-14 16:53:30 +02002173 return -ENODEV;
2174 }
Liron Kuch229090d2012-10-30 17:47:50 +02002175
2176 if (count < MIN_ACCEPTABLE_BUFFER_COUNT) {
2177 pr_err("%s: tspp requires a minimum of %i buffers\n",
2178 __func__, MIN_ACCEPTABLE_BUFFER_COUNT);
2179 return -EINVAL;
2180 }
2181
Joel Nider5bd73f82011-12-14 16:53:30 +02002182 channel = &pdev->channels[channel_id];
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002183
Liron Kuch229090d2012-10-30 17:47:50 +02002184 /* allow buffer allocation only if there was no previous buffer
2185 * allocation for this channel.
2186 */
2187 if (channel->buffer_count > 0) {
2188 pr_err("%s: buffers already allocated for channel %u",
2189 __func__, channel_id);
2190 return -EINVAL;
2191 }
Joel Nider5bd73f82011-12-14 16:53:30 +02002192
2193 channel->max_buffers = count;
2194
2195 /* set up interrupt frequency */
Liron Kuch229090d2012-10-30 17:47:50 +02002196 if (int_freq > channel->max_buffers) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002197 int_freq = channel->max_buffers;
Liron Kuch229090d2012-10-30 17:47:50 +02002198 pr_warn("%s: setting interrupt frequency to %u\n",
2199 __func__, int_freq);
Joel Nider5bd73f82011-12-14 16:53:30 +02002200 }
Liron Kuch229090d2012-10-30 17:47:50 +02002201 channel->int_freq = int_freq;
2202 /*
2203 * it is the responsibility of the caller to tspp_allocate_buffers(),
2204 * whether it's the user or the driver, to make sure the size parameter
2205 * is compatible to the channel mode.
2206 */
2207 channel->buffer_size = size;
Joel Nider5bd73f82011-12-14 16:53:30 +02002208
Liron Kuch229090d2012-10-30 17:47:50 +02002209 /* save user defined memory free function for later use */
2210 channel->memfree = memfree;
2211 channel->user_info = user;
2212
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002213 /*
2214 * For small buffers, create a DMA pool so that memory
2215 * is not wasted through dma_alloc_coherent.
2216 */
2217 if (TSPP_USE_DMA_POOL(channel->buffer_size)) {
2218 channel->dma_pool = dma_pool_create("tspp",
2219 NULL, channel->buffer_size, 0, 0);
2220 if (!channel->dma_pool) {
2221 pr_err("%s: Can't allocate memory pool\n", __func__);
2222 return -ENOMEM;
2223 }
2224 } else {
2225 channel->dma_pool = NULL;
2226 }
2227
2228
Liron Kuch229090d2012-10-30 17:47:50 +02002229 for (channel->buffer_count = 0;
2230 channel->buffer_count < channel->max_buffers;
Joel Nider5bd73f82011-12-14 16:53:30 +02002231 channel->buffer_count++) {
2232
2233 /* allocate the descriptor */
2234 struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
2235 kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
2236 if (!desc) {
Liron Kuch229090d2012-10-30 17:47:50 +02002237 pr_warn("%s: Can't allocate desc %i",
2238 __func__, channel->buffer_count);
Joel Nider5bd73f82011-12-14 16:53:30 +02002239 break;
2240 }
2241
2242 desc->desc.id = channel->buffer_count;
2243 /* allocate the buffer */
2244 if (tspp_alloc_buffer(channel_id, &desc->desc,
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002245 channel->buffer_size, channel->dma_pool,
2246 alloc, user) != 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002247 kfree(desc);
Liron Kuch229090d2012-10-30 17:47:50 +02002248 pr_warn("%s: Can't allocate buffer %i",
2249 __func__, channel->buffer_count);
Joel Nider5bd73f82011-12-14 16:53:30 +02002250 break;
2251 }
2252
2253 /* add the descriptor to the list */
2254 desc->filled = 0;
2255 desc->read_index = 0;
2256 if (!channel->data) {
2257 channel->data = desc;
2258 desc->next = channel->data;
2259 } else {
2260 last->next = desc;
2261 }
2262 last = desc;
2263 desc->next = channel->data;
2264
2265 /* prepare the sps descriptor */
2266 desc->sps.phys_base = desc->desc.phys_base;
2267 desc->sps.base = desc->desc.virt_base;
2268 desc->sps.size = desc->desc.size;
2269
2270 /* start the transfer */
2271 if (tspp_queue_buffer(channel, desc))
Liron Kuch229090d2012-10-30 17:47:50 +02002272 pr_err("%s: can't queue buffer %i",
2273 __func__, desc->desc.id);
2274 }
2275
2276 if (channel->buffer_count < channel->max_buffers) {
2277 /*
2278 * we failed to allocate the requested number of buffers.
2279 * we don't allow a partial success, so need to clean up here.
2280 */
2281 tspp_destroy_buffers(channel_id, channel);
2282 channel->buffer_count = 0;
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002283
2284 if (channel->dma_pool) {
2285 dma_pool_destroy(channel->dma_pool);
2286 channel->dma_pool = NULL;
2287 }
Liron Kuch229090d2012-10-30 17:47:50 +02002288 return -ENOMEM;
Joel Nider5bd73f82011-12-14 16:53:30 +02002289 }
2290
2291 channel->waiting = channel->data;
2292 channel->read = channel->data;
2293 channel->locked = channel->data;
Liron Kuch229090d2012-10-30 17:47:50 +02002294
Hamad Kadmany567bed82012-11-29 14:15:57 +02002295 /* Now that buffers are scheduled to HW, kick data expiration timer */
2296 if (channel->expiration_period_ms)
2297 mod_timer(&channel->expiration_timer,
2298 jiffies +
2299 MSEC_TO_JIFFIES(
2300 channel->expiration_period_ms));
2301
Joel Nider5bd73f82011-12-14 16:53:30 +02002302 return 0;
2303}
2304EXPORT_SYMBOL(tspp_allocate_buffers);
Joel Nider5556a852011-10-16 10:52:13 +02002305
2306/*** File Operations ***/
2307static ssize_t tspp_open(struct inode *inode, struct file *filp)
2308{
Joel Nider5bd73f82011-12-14 16:53:30 +02002309 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002310 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02002311
2312 TSPP_DEBUG("tspp_open");
Joel Nider5556a852011-10-16 10:52:13 +02002313 channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
2314 filp->private_data = channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02002315 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002316
2317 /* if this channel is already in use, quit */
2318 if (channel->used) {
2319 pr_err("tspp channel %i already in use",
2320 MINOR(channel->cdev.dev));
2321 return -EACCES;
2322 }
2323
Joel Nider5bd73f82011-12-14 16:53:30 +02002324 if (tspp_open_channel(dev, channel->id) != 0) {
Joel Nider5556a852011-10-16 10:52:13 +02002325 pr_err("tspp: error opening channel");
2326 return -EACCES;
2327 }
2328
2329 return 0;
2330}
2331
2332static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
2333{
2334 unsigned long flags;
2335 unsigned int mask = 0;
2336 struct tspp_channel *channel;
2337 channel = filp->private_data;
2338
2339 /* register the wait queue for this channel */
2340 poll_wait(filp, &channel->in_queue, p);
2341
2342 spin_lock_irqsave(&channel->pdev->spinlock, flags);
Joel Nider5bd73f82011-12-14 16:53:30 +02002343 if (channel->read &&
2344 channel->read->state == TSPP_BUF_STATE_DATA)
Joel Nider5556a852011-10-16 10:52:13 +02002345 mask = POLLIN | POLLRDNORM;
2346
2347 spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
2348
2349 return mask;
2350}
2351
2352static ssize_t tspp_release(struct inode *inode, struct file *filp)
2353{
Joel Nider5bd73f82011-12-14 16:53:30 +02002354 struct tspp_channel *channel = filp->private_data;
2355 u32 dev = channel->pdev->pdev->id;
2356 TSPP_DEBUG("tspp_release");
Joel Nider5556a852011-10-16 10:52:13 +02002357
Joel Nider5bd73f82011-12-14 16:53:30 +02002358 tspp_close_channel(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02002359
2360 return 0;
2361}
2362
2363static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
2364 loff_t *f_pos)
2365{
2366 size_t size = 0;
2367 size_t transferred = 0;
2368 struct tspp_channel *channel;
2369 struct tspp_mem_buffer *buffer;
2370 channel = filp->private_data;
2371
2372 TSPP_DEBUG("tspp_read");
Joel Nider5bd73f82011-12-14 16:53:30 +02002373
2374 while (!channel->read) {
2375 if (filp->f_flags & O_NONBLOCK) {
2376 pr_warn("tspp: no buffer on channel %i!",
2377 channel->id);
2378 return -EAGAIN;
2379 }
2380 /* go to sleep if there is nothing to read */
2381 if (wait_event_interruptible(channel->in_queue,
2382 (channel->read != NULL))) {
2383 pr_err("tspp: rude awakening\n");
2384 return -ERESTARTSYS;
2385 }
2386 }
2387
2388 buffer = channel->read;
2389
Joel Nider5556a852011-10-16 10:52:13 +02002390 /* see if we have any buffers ready to read */
2391 while (buffer->state != TSPP_BUF_STATE_DATA) {
2392 if (filp->f_flags & O_NONBLOCK) {
2393 pr_warn("tspp: nothing to read on channel %i!",
2394 channel->id);
2395 return -EAGAIN;
2396 }
2397 /* go to sleep if there is nothing to read */
Joel Nider5556a852011-10-16 10:52:13 +02002398 if (wait_event_interruptible(channel->in_queue,
2399 (buffer->state == TSPP_BUF_STATE_DATA))) {
2400 pr_err("tspp: rude awakening\n");
2401 return -ERESTARTSYS;
2402 }
2403 }
2404
2405 while (buffer->state == TSPP_BUF_STATE_DATA) {
2406 size = min(count, buffer->filled);
Joel Nider5556a852011-10-16 10:52:13 +02002407 if (size == 0)
2408 break;
2409
Joel Nider5bd73f82011-12-14 16:53:30 +02002410 if (copy_to_user(buf, buffer->desc.virt_base +
Joel Nider5556a852011-10-16 10:52:13 +02002411 buffer->read_index, size)) {
2412 pr_err("tspp: error copying to user buffer");
Joel Nider5bd73f82011-12-14 16:53:30 +02002413 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02002414 }
2415 buf += size;
2416 count -= size;
2417 transferred += size;
2418 buffer->read_index += size;
2419
Liron Kuch229090d2012-10-30 17:47:50 +02002420 /*
2421 * after reading the end of the buffer, requeue it,
2422 * and set up for reading the next one
2423 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002424 if (buffer->read_index == buffer->filled) {
Joel Nider5556a852011-10-16 10:52:13 +02002425 buffer->state = TSPP_BUF_STATE_WAITING;
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002426
Joel Nider5bd73f82011-12-14 16:53:30 +02002427 if (tspp_queue_buffer(channel, buffer))
2428 pr_err("tspp: can't submit transfer");
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002429
Joel Nider5bd73f82011-12-14 16:53:30 +02002430 channel->locked = channel->read;
2431 channel->read = channel->read->next;
Joel Nider5556a852011-10-16 10:52:13 +02002432 }
2433 }
2434
2435 return transferred;
2436}
2437
2438static long tspp_ioctl(struct file *filp,
2439 unsigned int param0, unsigned long param1)
2440{
Joel Nider5bd73f82011-12-14 16:53:30 +02002441 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002442 int rc = -1;
2443 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02002444 struct tspp_select_source ss;
2445 struct tspp_filter f;
2446 struct tspp_key k;
2447 struct tspp_iv iv;
2448 struct tspp_system_keys sk;
2449 struct tspp_buffer b;
Joel Nider5556a852011-10-16 10:52:13 +02002450 channel = filp->private_data;
Joel Nider5bd73f82011-12-14 16:53:30 +02002451 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002452
2453 if (!param1)
2454 return -EINVAL;
2455
2456 switch (param0) {
2457 case TSPP_IOCTL_SELECT_SOURCE:
Joel Nider5bd73f82011-12-14 16:53:30 +02002458 if (!access_ok(VERIFY_READ, param1,
2459 sizeof(struct tspp_select_source))) {
2460 return -EBUSY;
2461 }
2462 if (__copy_from_user(&ss, (void *)param1,
2463 sizeof(struct tspp_select_source)) == 0)
2464 rc = tspp_select_source(dev, channel->id, &ss);
Joel Nider5556a852011-10-16 10:52:13 +02002465 break;
2466 case TSPP_IOCTL_ADD_FILTER:
Joel Nider5bd73f82011-12-14 16:53:30 +02002467 if (!access_ok(VERIFY_READ, param1,
2468 sizeof(struct tspp_filter))) {
2469 return -ENOSR;
2470 }
2471 if (__copy_from_user(&f, (void *)param1,
2472 sizeof(struct tspp_filter)) == 0)
2473 rc = tspp_add_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002474 break;
2475 case TSPP_IOCTL_REMOVE_FILTER:
Joel Nider5bd73f82011-12-14 16:53:30 +02002476 if (!access_ok(VERIFY_READ, param1,
2477 sizeof(struct tspp_filter))) {
2478 return -EBUSY;
2479 }
2480 if (__copy_from_user(&f, (void *)param1,
2481 sizeof(struct tspp_filter)) == 0)
2482 rc = tspp_remove_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002483 break;
2484 case TSPP_IOCTL_SET_KEY:
Joel Nider5bd73f82011-12-14 16:53:30 +02002485 if (!access_ok(VERIFY_READ, param1,
2486 sizeof(struct tspp_key))) {
2487 return -EBUSY;
2488 }
2489 if (__copy_from_user(&k, (void *)param1,
2490 sizeof(struct tspp_key)) == 0)
2491 rc = tspp_set_key(dev, channel->id, &k);
Joel Nider5556a852011-10-16 10:52:13 +02002492 break;
2493 case TSPP_IOCTL_SET_IV:
Joel Nider5bd73f82011-12-14 16:53:30 +02002494 if (!access_ok(VERIFY_READ, param1,
2495 sizeof(struct tspp_iv))) {
2496 return -EBUSY;
2497 }
2498 if (__copy_from_user(&iv, (void *)param1,
2499 sizeof(struct tspp_iv)) == 0)
2500 rc = tspp_set_iv(channel, &iv);
Joel Nider5556a852011-10-16 10:52:13 +02002501 break;
2502 case TSPP_IOCTL_SET_SYSTEM_KEYS:
Joel Nider5bd73f82011-12-14 16:53:30 +02002503 if (!access_ok(VERIFY_READ, param1,
2504 sizeof(struct tspp_system_keys))) {
2505 return -EINVAL;
2506 }
2507 if (__copy_from_user(&sk, (void *)param1,
2508 sizeof(struct tspp_system_keys)) == 0)
2509 rc = tspp_set_system_keys(channel, &sk);
Joel Nider5556a852011-10-16 10:52:13 +02002510 break;
2511 case TSPP_IOCTL_BUFFER_SIZE:
Joel Nider5bd73f82011-12-14 16:53:30 +02002512 if (!access_ok(VERIFY_READ, param1,
2513 sizeof(struct tspp_buffer))) {
2514 rc = -EINVAL;
2515 }
2516 if (__copy_from_user(&b, (void *)param1,
2517 sizeof(struct tspp_buffer)) == 0)
2518 rc = tspp_set_buffer_size(channel, &b);
Joel Nider5556a852011-10-16 10:52:13 +02002519 break;
2520 default:
2521 pr_err("tspp: Unknown ioctl %i", param0);
2522 }
2523
Liron Kuch229090d2012-10-30 17:47:50 +02002524 /*
2525 * normalize the return code in case one of the subfunctions does
2526 * something weird
2527 */
Joel Nider5556a852011-10-16 10:52:13 +02002528 if (rc != 0)
Joel Nider5bd73f82011-12-14 16:53:30 +02002529 rc = -ENOIOCTLCMD;
Joel Nider5556a852011-10-16 10:52:13 +02002530
2531 return rc;
2532}
2533
2534/*** debugfs ***/
Joel Nider5556a852011-10-16 10:52:13 +02002535static int debugfs_iomem_x32_set(void *data, u64 val)
2536{
2537 writel_relaxed(val, data);
2538 wmb();
2539 return 0;
2540}
2541
2542static int debugfs_iomem_x32_get(void *data, u64 *val)
2543{
2544 *val = readl_relaxed(data);
2545 return 0;
2546}
2547
2548DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
2549 debugfs_iomem_x32_set, "0x%08llx");
2550
2551static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
2552 int instance)
2553{
2554 char name[10];
2555 snprintf(name, 10, "tsif%i", instance);
2556 tsif_device->dent_tsif = debugfs_create_dir(
2557 name, NULL);
2558 if (tsif_device->dent_tsif) {
2559 int i;
2560 void __iomem *base = tsif_device->base;
2561 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
2562 tsif_device->debugfs_tsif_regs[i] =
2563 debugfs_create_file(
2564 debugfs_tsif_regs[i].name,
2565 debugfs_tsif_regs[i].mode,
2566 tsif_device->dent_tsif,
2567 base + debugfs_tsif_regs[i].offset,
2568 &fops_iomem_x32);
2569 }
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02002570
2571 debugfs_create_u32(
2572 "stat_rx_chunks",
2573 S_IRUGO|S_IWUGO,
2574 tsif_device->dent_tsif,
2575 &tsif_device->stat_rx);
2576
2577 debugfs_create_u32(
2578 "stat_overflow",
2579 S_IRUGO|S_IWUGO,
2580 tsif_device->dent_tsif,
2581 &tsif_device->stat_overflow);
2582
2583 debugfs_create_u32(
2584 "stat_lost_sync",
2585 S_IRUGO|S_IWUGO,
2586 tsif_device->dent_tsif,
2587 &tsif_device->stat_lost_sync);
2588
2589 debugfs_create_u32(
2590 "stat_timeout",
2591 S_IRUGO|S_IWUGO,
2592 tsif_device->dent_tsif,
2593 &tsif_device->stat_timeout);
2594
Joel Nider5556a852011-10-16 10:52:13 +02002595 }
2596}
2597
2598static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
2599{
2600 if (tsif_device->dent_tsif) {
2601 int i;
2602 debugfs_remove_recursive(tsif_device->dent_tsif);
2603 tsif_device->dent_tsif = NULL;
2604 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
2605 tsif_device->debugfs_tsif_regs[i] = NULL;
2606 }
2607}
2608
2609static void tspp_debugfs_init(struct tspp_device *device, int instance)
2610{
2611 char name[10];
2612 snprintf(name, 10, "tspp%i", instance);
2613 device->dent = debugfs_create_dir(
2614 name, NULL);
2615 if (device->dent) {
2616 int i;
2617 void __iomem *base = device->base;
2618 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
2619 device->debugfs_regs[i] =
2620 debugfs_create_file(
2621 debugfs_tspp_regs[i].name,
2622 debugfs_tspp_regs[i].mode,
2623 device->dent,
2624 base + debugfs_tspp_regs[i].offset,
2625 &fops_iomem_x32);
2626 }
2627 }
2628}
2629
2630static void tspp_debugfs_exit(struct tspp_device *device)
2631{
2632 if (device->dent) {
2633 int i;
2634 debugfs_remove_recursive(device->dent);
2635 device->dent = NULL;
2636 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
2637 device->debugfs_regs[i] = NULL;
2638 }
2639}
Joel Nider5556a852011-10-16 10:52:13 +02002640
Liron Kuch8fa85b02013-01-01 18:29:47 +02002641/* copy device-tree data to platfrom data struct */
2642static __devinit struct msm_tspp_platform_data *
2643msm_tspp_dt_to_pdata(struct platform_device *pdev)
2644{
2645 struct device_node *node = pdev->dev.of_node;
2646 struct msm_tspp_platform_data *data;
2647 struct msm_gpio *gpios;
2648 int i, rc;
2649 int gpio;
2650 u32 gpio_func;
2651
2652 /* Note: memory allocated by devm_kzalloc is freed automatically */
2653 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2654 if (!data) {
2655 pr_err("tspp: Unable to allocate platform data\n");
2656 return NULL;
2657 }
2658 rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk);
2659 if (rc) {
2660 pr_err("tspp: Could not find tsif-pclk property, err = %d\n",
2661 rc);
2662 return NULL;
2663 }
2664 rc = of_property_read_string(node, "qcom,tsif-ref-clk",
2665 &data->tsif_ref_clk);
2666 if (rc) {
2667 pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n",
2668 rc);
2669 return NULL;
2670 }
2671
2672 data->num_gpios = of_gpio_count(node);
2673 if (data->num_gpios == 0) {
2674 pr_err("tspp: Could not find GPIO definitions\n");
2675 return NULL;
2676 }
2677 gpios = devm_kzalloc(&pdev->dev,
2678 (data->num_gpios * sizeof(struct msm_gpio)),
2679 GFP_KERNEL);
2680 if (!gpios) {
2681 pr_err("tspp: Unable to allocate memory for GPIOs table\n");
2682 return NULL;
2683 }
2684 /* Assuming GPIO FUNC property is the same for all GPIOs */
2685 if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) {
2686 pr_err("tspp: Could not find gpios-func property\n");
2687 return NULL;
2688 }
2689 for (i = 0; i < data->num_gpios; i++) {
2690 gpio = of_get_gpio(node, i);
2691 gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func,
2692 GPIO_CFG_INPUT,
2693 GPIO_CFG_PULL_DOWN,
2694 GPIO_CFG_2MA);
2695 rc = of_property_read_string_index(node, "qcom,gpio-names",
2696 i, &gpios[i].label);
2697 if (rc)
2698 pr_warn("tspp: Could not find gpio-names property\n");
2699 }
2700
2701 data->gpios = gpios;
2702
2703 return data;
2704}
2705
2706static int msm_tspp_map_irqs(struct platform_device *pdev,
2707 struct tspp_device *device)
2708{
2709 int rc;
2710 int i;
2711
2712 /* get IRQ numbers from platform information */
2713
2714 /* map TSPP IRQ */
2715 rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ");
2716 if (rc > 0) {
2717 device->tspp_irq = rc;
2718 rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
2719 dev_name(&pdev->dev), device);
2720 if (rc) {
2721 dev_err(&pdev->dev,
2722 "failed to request TSPP IRQ %d : %d",
2723 device->tspp_irq, rc);
2724 device->tspp_irq = 0;
2725 return -EINVAL;
2726 }
2727 } else {
2728 dev_err(&pdev->dev, "failed to get TSPP IRQ");
2729 return -EINVAL;
2730 }
2731
2732 /* map TSIF IRQs */
2733 rc = platform_get_irq_byname(pdev, "TSIF0_IRQ");
2734 if (rc > 0) {
2735 device->tsif[0].tsif_irq = rc;
2736 } else {
2737 dev_err(&pdev->dev, "failed to get TSIF0 IRQ");
2738 return -EINVAL;
2739 }
2740
2741 rc = platform_get_irq_byname(pdev, "TSIF1_IRQ");
2742 if (rc > 0) {
2743 device->tsif[1].tsif_irq = rc;
2744 } else {
2745 dev_err(&pdev->dev, "failed to get TSIF1 IRQ");
2746 return -EINVAL;
2747 }
2748
2749 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2750 rc = request_irq(device->tsif[i].tsif_irq,
2751 tsif_isr, IRQF_SHARED,
2752 dev_name(&pdev->dev), &device->tsif[i]);
2753 if (rc) {
2754 dev_warn(&pdev->dev, "failed to request TSIF%d IRQ: %d",
2755 i, rc);
2756 device->tsif[i].tsif_irq = 0;
2757 }
2758 }
2759
2760 /* map BAM IRQ */
2761 rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ");
2762 if (rc > 0) {
2763 device->bam_irq = rc;
2764 } else {
2765 dev_err(&pdev->dev, "failed to get TSPP BAM IRQ");
2766 return -EINVAL;
2767 }
2768
2769 return 0;
2770}
2771
Joel Nider5556a852011-10-16 10:52:13 +02002772static int __devinit msm_tspp_probe(struct platform_device *pdev)
2773{
2774 int rc = -ENODEV;
2775 u32 version;
Liron Kuch229090d2012-10-30 17:47:50 +02002776 u32 i, j;
Joel Nider5556a852011-10-16 10:52:13 +02002777 struct msm_tspp_platform_data *data;
2778 struct tspp_device *device;
2779 struct resource *mem_tsif0;
2780 struct resource *mem_tsif1;
2781 struct resource *mem_tspp;
2782 struct resource *mem_bam;
Liron Kuch229090d2012-10-30 17:47:50 +02002783 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02002784
Liron Kuch8fa85b02013-01-01 18:29:47 +02002785 if (pdev->dev.of_node) {
2786 /* get information from device tree */
2787 data = msm_tspp_dt_to_pdata(pdev);
2788 /* get device ID */
2789 rc = of_property_read_u32(pdev->dev.of_node,
2790 "cell-index", &pdev->id);
2791 if (rc)
2792 pdev->id = -1;
2793
2794 pdev->dev.platform_data = data;
2795 } else {
2796 /* must have platform data */
2797 data = pdev->dev.platform_data;
2798 }
Joel Nider5556a852011-10-16 10:52:13 +02002799 if (!data) {
2800 pr_err("tspp: Platform data not available");
2801 rc = -EINVAL;
2802 goto out;
2803 }
2804
2805 /* check for valid device id */
Joel Nider5bd73f82011-12-14 16:53:30 +02002806 if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
Joel Nider5556a852011-10-16 10:52:13 +02002807 pr_err("tspp: Invalid device ID %d", pdev->id);
2808 rc = -EINVAL;
2809 goto out;
2810 }
2811
2812 /* OK, we will use this device */
2813 device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
2814 if (!device) {
2815 pr_err("tspp: Failed to allocate memory for device");
2816 rc = -ENOMEM;
2817 goto out;
2818 }
2819
2820 /* set up references */
2821 device->pdev = pdev;
2822 platform_set_drvdata(pdev, device);
2823
2824 /* map clocks */
2825 if (data->tsif_pclk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002826 device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
Joel Nider5556a852011-10-16 10:52:13 +02002827 if (IS_ERR(device->tsif_pclk)) {
2828 pr_err("tspp: failed to get %s",
2829 data->tsif_pclk);
2830 rc = PTR_ERR(device->tsif_pclk);
2831 device->tsif_pclk = NULL;
2832 goto err_pclock;
2833 }
2834 }
2835 if (data->tsif_ref_clk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002836 device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
Joel Nider5556a852011-10-16 10:52:13 +02002837 if (IS_ERR(device->tsif_ref_clk)) {
2838 pr_err("tspp: failed to get %s",
2839 data->tsif_ref_clk);
2840 rc = PTR_ERR(device->tsif_ref_clk);
2841 device->tsif_ref_clk = NULL;
2842 goto err_refclock;
2843 }
2844 }
2845
2846 /* map I/O memory */
Liron Kuch8fa85b02013-01-01 18:29:47 +02002847 mem_tsif0 = platform_get_resource_byname(pdev,
2848 IORESOURCE_MEM, "MSM_TSIF0_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002849 if (!mem_tsif0) {
2850 pr_err("tspp: Missing tsif0 MEM resource");
2851 rc = -ENXIO;
2852 goto err_res_tsif0;
2853 }
2854 device->tsif[0].base = ioremap(mem_tsif0->start,
2855 resource_size(mem_tsif0));
2856 if (!device->tsif[0].base) {
2857 pr_err("tspp: ioremap failed");
2858 goto err_map_tsif0;
2859 }
2860
Liron Kuch8fa85b02013-01-01 18:29:47 +02002861 mem_tsif1 = platform_get_resource_byname(pdev,
2862 IORESOURCE_MEM, "MSM_TSIF1_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002863 if (!mem_tsif1) {
2864 dev_err(&pdev->dev, "Missing tsif1 MEM resource");
2865 rc = -ENXIO;
2866 goto err_res_tsif1;
2867 }
2868 device->tsif[1].base = ioremap(mem_tsif1->start,
2869 resource_size(mem_tsif1));
2870 if (!device->tsif[1].base) {
2871 dev_err(&pdev->dev, "ioremap failed");
2872 goto err_map_tsif1;
2873 }
2874
Liron Kuch8fa85b02013-01-01 18:29:47 +02002875 mem_tspp = platform_get_resource_byname(pdev,
2876 IORESOURCE_MEM, "MSM_TSPP_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002877 if (!mem_tspp) {
2878 dev_err(&pdev->dev, "Missing MEM resource");
2879 rc = -ENXIO;
2880 goto err_res_dev;
2881 }
2882 device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
2883 if (!device->base) {
2884 dev_err(&pdev->dev, "ioremap failed");
2885 goto err_map_dev;
2886 }
2887
Liron Kuch8fa85b02013-01-01 18:29:47 +02002888 mem_bam = platform_get_resource_byname(pdev,
2889 IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002890 if (!mem_bam) {
2891 pr_err("tspp: Missing bam MEM resource");
2892 rc = -ENXIO;
2893 goto err_res_bam;
2894 }
2895 memset(&device->bam_props, 0, sizeof(device->bam_props));
2896 device->bam_props.phys_addr = mem_bam->start;
2897 device->bam_props.virt_addr = ioremap(mem_bam->start,
2898 resource_size(mem_bam));
2899 if (!device->bam_props.virt_addr) {
2900 dev_err(&pdev->dev, "ioremap failed");
2901 goto err_map_bam;
2902 }
2903
Liron Kuch8fa85b02013-01-01 18:29:47 +02002904 if (msm_tspp_map_irqs(pdev, device))
Joel Nider5556a852011-10-16 10:52:13 +02002905 goto err_irq;
Joel Nider5556a852011-10-16 10:52:13 +02002906
Joel Nider5556a852011-10-16 10:52:13 +02002907 /* power management */
2908 pm_runtime_set_active(&pdev->dev);
2909 pm_runtime_enable(&pdev->dev);
2910
Joel Nider5556a852011-10-16 10:52:13 +02002911 tspp_debugfs_init(device, 0);
2912
2913 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2914 tsif_debugfs_init(&device->tsif[i], i);
Joel Nider5556a852011-10-16 10:52:13 +02002915
2916 wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
2917 dev_name(&pdev->dev));
2918
2919 /* set up pointers to ram-based 'registers' */
Joel Nider5bd73f82011-12-14 16:53:30 +02002920 device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
2921 device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
2922 device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
2923 device->tspp_key_table = device->base + TSPP_DATA_KEY;
2924 device->tspp_global_performance =
2925 device->base + TSPP_GLOBAL_PERFORMANCE;
2926 device->tspp_pipe_context =
2927 device->base + TSPP_PIPE_CONTEXT;
2928 device->tspp_pipe_performance =
2929 device->base + TSPP_PIPE_PERFORMANCE;
Joel Nider5556a852011-10-16 10:52:13 +02002930
2931 device->bam_props.summing_threshold = 0x10;
2932 device->bam_props.irq = device->bam_irq;
2933 device->bam_props.manage = SPS_BAM_MGR_LOCAL;
2934
Liron Kuch8fa85b02013-01-01 18:29:47 +02002935 if (tspp_clock_start(device) != 0) {
2936 dev_err(&pdev->dev, "Can't start clocks");
2937 goto err_clock;
2938 }
2939
Joel Nider5556a852011-10-16 10:52:13 +02002940 if (sps_register_bam_device(&device->bam_props,
2941 &device->bam_handle) != 0) {
2942 pr_err("tspp: failed to register bam");
2943 goto err_bam;
2944 }
2945
Joel Nider5556a852011-10-16 10:52:13 +02002946 spin_lock_init(&device->spinlock);
2947 tasklet_init(&device->tlet, tspp_sps_complete_tlet,
2948 (unsigned long)device);
2949
2950 /* initialize everything to a known state */
2951 tspp_global_reset(device);
2952
2953 version = readl_relaxed(device->base + TSPP_VERSION);
Liron Kuch8fa85b02013-01-01 18:29:47 +02002954 /*
2955 * TSPP version can be bits [7:0] or alternatively,
2956 * TSPP major version is bits [31:28].
2957 */
2958 if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1))
Joel Nider5556a852011-10-16 10:52:13 +02002959 pr_warn("tspp: unrecognized hw version=%i", version);
2960
Joel Nider5bd73f82011-12-14 16:53:30 +02002961 /* initialize the channels */
Joel Nider5556a852011-10-16 10:52:13 +02002962 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002963 if (tspp_channel_init(&(device->channels[i]), device) != 0) {
2964 pr_err("tspp_channel_init failed");
2965 goto err_channel;
2966 }
Joel Nider5556a852011-10-16 10:52:13 +02002967 }
2968
Joel Nider5bd73f82011-12-14 16:53:30 +02002969 /* stop the clocks for power savings */
2970 tspp_clock_stop(device);
2971
2972 /* everything is ok, so add the device to the list */
2973 list_add_tail(&(device->devlist), &tspp_devices);
2974
Joel Nider5556a852011-10-16 10:52:13 +02002975 return 0;
2976
Joel Nider5bd73f82011-12-14 16:53:30 +02002977err_channel:
Liron Kuch8fa85b02013-01-01 18:29:47 +02002978 /* un-initialize channels */
Liron Kuch229090d2012-10-30 17:47:50 +02002979 for (j = 0; j < i; j++) {
2980 channel = &(device->channels[i]);
2981 device_destroy(tspp_class, channel->cdev.dev);
2982 cdev_del(&channel->cdev);
2983 }
Liron Kuch8fa85b02013-01-01 18:29:47 +02002984
Joel Nider5556a852011-10-16 10:52:13 +02002985 sps_deregister_bam_device(device->bam_handle);
Liron Kuch8fa85b02013-01-01 18:29:47 +02002986err_clock:
Joel Nider5556a852011-10-16 10:52:13 +02002987err_bam:
Joel Nider5556a852011-10-16 10:52:13 +02002988 tspp_debugfs_exit(device);
2989 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2990 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02002991err_irq:
Liron Kuch8fa85b02013-01-01 18:29:47 +02002992 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2993 if (device->tsif[i].tsif_irq)
2994 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
2995 }
2996 if (device->tspp_irq)
2997 free_irq(device->tspp_irq, device);
2998
Joel Nider5556a852011-10-16 10:52:13 +02002999 iounmap(device->bam_props.virt_addr);
3000err_map_bam:
3001err_res_bam:
3002 iounmap(device->base);
3003err_map_dev:
3004err_res_dev:
3005 iounmap(device->tsif[1].base);
3006err_map_tsif1:
3007err_res_tsif1:
3008 iounmap(device->tsif[0].base);
3009err_map_tsif0:
3010err_res_tsif0:
3011 if (device->tsif_ref_clk)
3012 clk_put(device->tsif_ref_clk);
3013err_refclock:
3014 if (device->tsif_pclk)
3015 clk_put(device->tsif_pclk);
3016err_pclock:
3017 kfree(device);
3018
3019out:
3020 return rc;
3021}
3022
3023static int __devexit msm_tspp_remove(struct platform_device *pdev)
3024{
Joel Nider5bd73f82011-12-14 16:53:30 +02003025 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02003026 u32 i;
Liron Kuchde8cbf92013-02-21 14:25:57 +02003027 int rc;
Joel Nider5556a852011-10-16 10:52:13 +02003028
3029 struct tspp_device *device = platform_get_drvdata(pdev);
3030
Joel Nider5bd73f82011-12-14 16:53:30 +02003031 /* free the buffers, and delete the channels */
3032 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
3033 channel = &device->channels[i];
3034 tspp_close_channel(device->pdev->id, i);
3035 device_destroy(tspp_class, channel->cdev.dev);
3036 cdev_del(&channel->cdev);
3037 }
3038
Liron Kuch8fa85b02013-01-01 18:29:47 +02003039 /* de-registering BAM device requires clocks */
Liron Kuchde8cbf92013-02-21 14:25:57 +02003040 rc = tspp_clock_start(device);
3041 if (rc == 0) {
3042 sps_deregister_bam_device(device->bam_handle);
3043 tspp_clock_stop(device);
3044 }
Joel Nider5556a852011-10-16 10:52:13 +02003045
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02003046 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
Joel Nider5556a852011-10-16 10:52:13 +02003047 tsif_debugfs_exit(&device->tsif[i]);
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02003048 if (device->tsif[i].tsif_irq)
3049 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
3050 }
Joel Nider5556a852011-10-16 10:52:13 +02003051
3052 wake_lock_destroy(&device->wake_lock);
3053 free_irq(device->tspp_irq, device);
Joel Nider5556a852011-10-16 10:52:13 +02003054
3055 iounmap(device->bam_props.virt_addr);
3056 iounmap(device->base);
3057 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3058 iounmap(device->tsif[i].base);
3059
3060 if (device->tsif_ref_clk)
3061 clk_put(device->tsif_ref_clk);
3062
3063 if (device->tsif_pclk)
3064 clk_put(device->tsif_pclk);
3065
3066 pm_runtime_disable(&pdev->dev);
Liron Kuchde8cbf92013-02-21 14:25:57 +02003067
Joel Nider5556a852011-10-16 10:52:13 +02003068 kfree(device);
3069
3070 return 0;
3071}
3072
3073/*** power management ***/
3074
3075static int tspp_runtime_suspend(struct device *dev)
3076{
3077 dev_dbg(dev, "pm_runtime: suspending...");
3078 return 0;
3079}
3080
3081static int tspp_runtime_resume(struct device *dev)
3082{
3083 dev_dbg(dev, "pm_runtime: resuming...");
3084 return 0;
3085}
3086
3087static const struct dev_pm_ops tspp_dev_pm_ops = {
3088 .runtime_suspend = tspp_runtime_suspend,
3089 .runtime_resume = tspp_runtime_resume,
3090};
3091
Liron Kuch8fa85b02013-01-01 18:29:47 +02003092static struct of_device_id msm_match_table[] = {
3093 {.compatible = "qcom,msm_tspp"},
3094 {}
3095};
3096
Joel Nider5556a852011-10-16 10:52:13 +02003097static struct platform_driver msm_tspp_driver = {
3098 .probe = msm_tspp_probe,
3099 .remove = __exit_p(msm_tspp_remove),
3100 .driver = {
3101 .name = "msm_tspp",
3102 .pm = &tspp_dev_pm_ops,
Liron Kuch8fa85b02013-01-01 18:29:47 +02003103 .of_match_table = msm_match_table,
Joel Nider5556a852011-10-16 10:52:13 +02003104 },
3105};
3106
3107
3108static int __init mod_init(void)
3109{
Joel Nider5556a852011-10-16 10:52:13 +02003110 int rc;
3111
Joel Nider5bd73f82011-12-14 16:53:30 +02003112 /* make the char devs (channels) */
Joel Nider5556a852011-10-16 10:52:13 +02003113 rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
3114 if (rc) {
3115 pr_err("tspp: alloc_chrdev_region failed: %d", rc);
3116 goto err_devrgn;
3117 }
3118
3119 tspp_class = class_create(THIS_MODULE, "tspp");
3120 if (IS_ERR(tspp_class)) {
3121 rc = PTR_ERR(tspp_class);
3122 pr_err("tspp: Error creating class: %d", rc);
3123 goto err_class;
3124 }
3125
Joel Nider5bd73f82011-12-14 16:53:30 +02003126 /* register the driver, and check hardware */
3127 rc = platform_driver_register(&msm_tspp_driver);
3128 if (rc) {
3129 pr_err("tspp: platform_driver_register failed: %d", rc);
3130 goto err_register;
Joel Nider5556a852011-10-16 10:52:13 +02003131 }
3132
3133 return 0;
3134
Joel Nider5bd73f82011-12-14 16:53:30 +02003135err_register:
3136 class_destroy(tspp_class);
Joel Nider5556a852011-10-16 10:52:13 +02003137err_class:
3138 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
3139err_devrgn:
Joel Nider5556a852011-10-16 10:52:13 +02003140 return rc;
3141}
3142
3143static void __exit mod_exit(void)
3144{
Joel Nider5bd73f82011-12-14 16:53:30 +02003145 /* delete low level driver */
3146 platform_driver_unregister(&msm_tspp_driver);
Joel Nider5556a852011-10-16 10:52:13 +02003147
Joel Nider5bd73f82011-12-14 16:53:30 +02003148 /* delete upper layer interface */
Joel Nider5556a852011-10-16 10:52:13 +02003149 class_destroy(tspp_class);
3150 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
Joel Nider5556a852011-10-16 10:52:13 +02003151}
3152
3153module_init(mod_init);
3154module_exit(mod_exit);
3155
Joel Nider5bd73f82011-12-14 16:53:30 +02003156MODULE_DESCRIPTION("TSPP platform device and char dev");
Joel Nider5556a852011-10-16 10:52:13 +02003157MODULE_LICENSE("GPL v2");