blob: ef238718611323ce7afde92b76f8ee6fb9919079 [file] [log] [blame]
Hamad Kadmany090709b2013-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-12-14 16:53:30 +020026#include <linux/ioport.h> /* XXX_ mem_region */
27#include <linux/dma-mapping.h> /* dma_XXX */
Hamad Kadmany090709b2013-01-06 12:08:13 +020028#include <linux/dmapool.h> /* DMA pools */
Joel Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-12-14 16:53:30 +020038#include <linux/wakelock.h> /* Locking functions */
Hamad Kadmany81cee052012-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 Kuch59339922013-01-01 18:29:47 +020044#include <linux/of.h>
45#include <linux/of_gpio.h>
Liron Kuch275c0b32013-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 Kuch275c0b32013-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 Nider435ad8e2011-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 Kadmany81cee052012-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 Kadmany81cee052012-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 Kadmany090709b2013-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 Kadmany81cee052012-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Kuch72b78552012-10-30 17:47:50 +0200134#define TSPP_RST 0x00
Joel Nider5556a852011-10-16 10:52:13 +0200135#define TSPP_CLK_CONTROL 0x04
Liron Kuch72b78552012-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 Kuch72b78552012-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 Kuch72b78552012-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 Kuch72b78552012-10-30 17:47:50 +0200154#define TSPP_VERSION 0xAC
Joel Nider5556a852011-10-16 10:52:13 +0200155#define TSPP_GENERICS 0xB0
Liron Kuch72b78552012-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 Kuch72b78552012-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 Kuch72b78552012-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 Kuch72b78552012-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 Kuch72b78552012-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 Kuch72b78552012-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 Kuch72b78552012-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 Kadmany81cee052012-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 Nider435ad8e2011-12-14 16:53:30 +0200355 enum tspp_tsif_mode mode;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200356 int clock_inverse;
357 int data_inverse;
358 int sync_inverse;
359 int enable_inverse;
Hamad Kadmany44307d32012-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 Kadmany44307d32012-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Kadmany81cee052012-11-29 14:15:57 +0200412 u32 expiration_period_ms; /* notification on partially filled buffers */
413 struct timer_list expiration_timer;
Hamad Kadmany090709b2013-01-06 12:08:13 +0200414 struct dma_pool *dma_pool;
Liron Kuch72b78552012-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-12-14 16:53:30 +0200487static irqreturn_t tspp_isr(int irq, void *dev)
Joel Nider5556a852011-10-16 10:52:13 +0200488{
Joel Nider435ad8e2011-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 Nider435ad8e2011-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 Kadmany44307d32012-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 Nider435ad8e2011-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 Kadmany81cee052012-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 Nider435ad8e2011-12-14 16:53:30 +0200595 channel = &device->channels[i];
Hamad Kadmany81cee052012-11-29 14:15:57 +0200596
Joel Nider435ad8e2011-12-14 16:53:30 +0200597 if (!channel->used || !channel->waiting)
598 continue;
Joel Nider5556a852011-10-16 10:52:13 +0200599
Hamad Kadmany81cee052012-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-12-14 16:53:30 +0200616 channel->waiting->sps.phys_base);
Joel Nider5556a852011-10-16 10:52:13 +0200617
618 complete = 1;
Joel Nider435ad8e2011-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 Kadmany44307d32012-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Nider435ad8e2011-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 Kadmany81cee052012-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 Kuch275c0b32013-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 Kuch59339922013-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 Kuch59339922013-01-01 18:29:47 +0200665
Liron Kuch275c0b32013-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 Kuch59339922013-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 Kuch72b78552012-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 Kuch275c0b32013-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 Kuch275c0b32013-02-10 15:19:32 +0200694 int i;
Joel Nider5556a852011-10-16 10:52:13 +0200695 const struct msm_gpio *g;
Liron Kuch59339922013-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 Kuch275c0b32013-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 Kuch72b78552012-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 Kuch275c0b32013-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 Kuch275c0b32013-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 Kuch275c0b32013-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 Kuch59339922013-01-01 18:29:47 +0200732
Liron Kuch275c0b32013-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 Kuch275c0b32013-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 Kuch59339922013-01-01 18:29:47 +0200755
Liron Kuch275c0b32013-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 Nider435ad8e2011-12-14 16:53:30 +0200763/*** Clock functions ***/
764static int tspp_clock_start(struct tspp_device *device)
765{
766 if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
767 pr_err("tspp: Can't start pclk");
768 return -EBUSY;
769 }
770
771 if (device->tsif_ref_clk &&
772 clk_prepare_enable(device->tsif_ref_clk) != 0) {
773 pr_err("tspp: Can't start ref clk");
774 clk_disable_unprepare(device->tsif_pclk);
775 return -EBUSY;
776 }
777
778 return 0;
779}
780
781static void tspp_clock_stop(struct tspp_device *device)
782{
783 if (device->tsif_pclk)
784 clk_disable(device->tsif_pclk);
785
786 if (device->tsif_ref_clk)
787 clk_disable(device->tsif_ref_clk);
788}
789
Joel Nider5556a852011-10-16 10:52:13 +0200790/*** TSIF functions ***/
791static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
792{
793 int start_hardware = 0;
794 u32 ctl;
795
796 if (tsif_device->ref_count == 0) {
797 start_hardware = 1;
798 } else if (tsif_device->ref_count > 0) {
799 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
800 if ((ctl & TSIF_STS_CTL_START) != 1) {
801 /* this hardware should already be running */
802 pr_warn("tspp: tsif hw not started but ref count > 0");
803 start_hardware = 1;
804 }
805 }
806
807 if (start_hardware) {
Joel Nider435ad8e2011-12-14 16:53:30 +0200808 ctl = TSIF_STS_CTL_EN_IRQ |
Joel Nider5556a852011-10-16 10:52:13 +0200809 TSIF_STS_CTL_EN_DM;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200810
811 if (tsif_device->clock_inverse)
812 ctl |= TSIF_STS_CTL_INV_CLOCK;
813
814 if (tsif_device->data_inverse)
815 ctl |= TSIF_STS_CTL_INV_DATA;
816
817 if (tsif_device->sync_inverse)
818 ctl |= TSIF_STS_CTL_INV_SYNC;
819
820 if (tsif_device->enable_inverse)
821 ctl |= TSIF_STS_CTL_INV_ENABLE;
822
Joel Nider435ad8e2011-12-14 16:53:30 +0200823 switch (tsif_device->mode) {
824 case TSPP_TSIF_MODE_LOOPBACK:
825 ctl |= TSIF_STS_CTL_EN_NULL |
826 TSIF_STS_CTL_EN_ERROR |
827 TSIF_STS_CTL_TEST_MODE;
828 break;
829 case TSPP_TSIF_MODE_1:
830 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
831 TSIF_STS_CTL_EN_TCR;
832 break;
833 case TSPP_TSIF_MODE_2:
834 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
835 TSIF_STS_CTL_EN_TCR |
836 TSIF_STS_CTL_MODE_2;
837 break;
838 default:
839 pr_warn("tspp: unknown tsif mode 0x%x",
840 tsif_device->mode);
Joel Nider5556a852011-10-16 10:52:13 +0200841 }
842 writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
843 writel_relaxed(tsif_device->time_limit,
844 tsif_device->base + TSIF_TIME_LIMIT_OFF);
845 wmb();
846 writel_relaxed(ctl | TSIF_STS_CTL_START,
847 tsif_device->base + TSIF_STS_CTL_OFF);
848 wmb();
Joel Nider5556a852011-10-16 10:52:13 +0200849 }
850
Joel Nider435ad8e2011-12-14 16:53:30 +0200851 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
Joel Nider5556a852011-10-16 10:52:13 +0200852 tsif_device->ref_count++;
853
Joel Nider435ad8e2011-12-14 16:53:30 +0200854 return (ctl & TSIF_STS_CTL_START) ? 0 : -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200855}
856
857static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
858{
859 if (tsif_device->ref_count == 0)
860 return;
861
862 tsif_device->ref_count--;
863
864 if (tsif_device->ref_count == 0) {
865 writel_relaxed(TSIF_STS_CTL_STOP,
866 tsif_device->base + TSIF_STS_CTL_OFF);
867 wmb();
868 }
869}
870
Joel Nider435ad8e2011-12-14 16:53:30 +0200871/*** local TSPP functions ***/
872static int tspp_channels_in_use(struct tspp_device *pdev)
873{
874 int i;
875 int count = 0;
876 for (i = 0; i < TSPP_NUM_CHANNELS; i++)
877 count += (pdev->channels[i].used ? 1 : 0);
878
879 return count;
880}
881
882static struct tspp_device *tspp_find_by_id(int id)
883{
884 struct tspp_device *dev;
885 list_for_each_entry(dev, &tspp_devices, devlist) {
886 if (dev->pdev->id == id)
887 return dev;
888 }
889 return NULL;
890}
891
Joel Nider5556a852011-10-16 10:52:13 +0200892static int tspp_get_key_entry(void)
893{
894 int i;
895 for (i = 0; i < TSPP_NUM_KEYS; i++) {
896 if (!(tspp_key_entry & (1 << i))) {
897 tspp_key_entry |= (1 << i);
898 return i;
899 }
900 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200901 return 1 < TSPP_NUM_KEYS;
Joel Nider5556a852011-10-16 10:52:13 +0200902}
903
904static void tspp_free_key_entry(int entry)
905{
906 if (entry > TSPP_NUM_KEYS) {
907 pr_err("tspp_free_key_entry: index out of bounds");
908 return;
909 }
910
911 tspp_key_entry &= ~(1 << entry);
912}
913
Joel Nider435ad8e2011-12-14 16:53:30 +0200914static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +0200915 u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user)
Joel Nider5556a852011-10-16 10:52:13 +0200916{
Joel Nider435ad8e2011-12-14 16:53:30 +0200917 if (size < TSPP_MIN_BUFFER_SIZE ||
918 size > TSPP_MAX_BUFFER_SIZE) {
919 pr_err("tspp: bad buffer size %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200920 return -ENOMEM;
921 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200922
923 if (alloc) {
924 TSPP_DEBUG("tspp using alloc function");
925 desc->virt_base = alloc(channel_id, size,
926 &desc->phys_base, user);
927 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +0200928 if (!dma_pool)
929 desc->virt_base = dma_alloc_coherent(NULL, size,
930 &desc->phys_base, GFP_KERNEL);
931 else
932 desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL,
933 &desc->phys_base);
934
Liron Kuch72b78552012-10-30 17:47:50 +0200935 if (desc->virt_base == 0) {
Hamad Kadmany090709b2013-01-06 12:08:13 +0200936 pr_err("tspp: dma buffer allocation failed %i\n", size);
Liron Kuch72b78552012-10-30 17:47:50 +0200937 return -ENOMEM;
938 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200939 }
940
941 desc->size = size;
942 return 0;
943}
944
945static int tspp_queue_buffer(struct tspp_channel *channel,
946 struct tspp_mem_buffer *buffer)
947{
948 int rc;
949 u32 flags = 0;
950
951 /* make sure the interrupt frequency is valid */
952 if (channel->int_freq < 1)
953 channel->int_freq = 1;
954
955 /* generate interrupt according to requested frequency */
956 if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
Hamad Kadmany81cee052012-11-29 14:15:57 +0200957 flags = SPS_IOVEC_FLAG_INT;
Joel Nider435ad8e2011-12-14 16:53:30 +0200958
959 /* start the transfer */
960 rc = sps_transfer_one(channel->pipe,
961 buffer->sps.phys_base,
962 buffer->sps.size,
963 channel->pdev,
964 flags);
965 if (rc < 0)
966 return rc;
967
968 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5556a852011-10-16 10:52:13 +0200969
970 return 0;
971}
972
973static int tspp_global_reset(struct tspp_device *pdev)
974{
975 u32 i, val;
976
977 /* stop all TSIFs */
978 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
979 pdev->tsif[i].ref_count = 1; /* allows stopping hw */
980 tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
981 pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200982 pdev->tsif[i].clock_inverse = 0;
983 pdev->tsif[i].data_inverse = 0;
984 pdev->tsif[i].sync_inverse = 0;
985 pdev->tsif[i].enable_inverse = 0;
Joel Nider5556a852011-10-16 10:52:13 +0200986 }
987 writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
988 wmb();
989
990 /* BAM */
991 if (sps_device_reset(pdev->bam_handle) != 0) {
992 pr_err("tspp: error resetting bam");
Joel Nider435ad8e2011-12-14 16:53:30 +0200993 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200994 }
995
996 /* TSPP tables */
997 for (i = 0; i < TSPP_FILTER_TABLES; i++)
Joel Nider435ad8e2011-12-14 16:53:30 +0200998 memset(pdev->filters[i],
Joel Nider5556a852011-10-16 10:52:13 +0200999 0, sizeof(struct tspp_pid_filter_table));
1000
1001 /* disable all filters */
1002 val = (2 << TSPP_NUM_CHANNELS) - 1;
1003 writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
1004
1005 /* TSPP registers */
1006 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1007 writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1008 pdev->base + TSPP_CONTROL);
1009 wmb();
Joel Nider435ad8e2011-12-14 16:53:30 +02001010 memset(pdev->tspp_global_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001011 sizeof(struct tspp_global_performance_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001012 memset(pdev->tspp_pipe_context, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001013 sizeof(struct tspp_pipe_context_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001014 memset(pdev->tspp_pipe_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001015 sizeof(struct tspp_pipe_performance_regs));
1016 wmb();
1017 writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1018 pdev->base + TSPP_CONTROL);
1019 wmb();
1020
1021 val = readl_relaxed(pdev->base + TSPP_CONFIG);
1022 val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
1023 TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
1024 TSPP_CONFIG_PS_CONT_ERR_MASK);
1025 TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
1026 writel_relaxed(val, pdev->base + TSPP_CONFIG);
Hamad Kadmany6bac7832012-12-20 18:30:40 +02001027 writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK);
Joel Nider5556a852011-10-16 10:52:13 +02001028 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
1029 writel_relaxed(0, pdev->base + TSPP_RST);
1030 wmb();
1031
1032 tspp_key_entry = 0;
1033
1034 return 0;
1035}
1036
Joel Nider435ad8e2011-12-14 16:53:30 +02001037static int tspp_select_source(u32 dev, u32 channel_id,
1038 struct tspp_select_source *src)
1039{
1040 /* make sure the requested src id is in bounds */
1041 if (src->source > TSPP_SOURCE_MEM) {
1042 pr_err("tspp source out of bounds");
1043 return -EINVAL;
1044 }
1045
1046 /* open the stream */
Hamad Kadmany92705b32012-10-23 14:15:41 +02001047 tspp_open_stream(dev, channel_id, src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001048
1049 return 0;
1050}
1051
1052static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
1053{
1054 struct tspp_device *pdev = channel->pdev;
1055
1056 writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
1057 writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
1058 return 0;
1059}
1060
1061static int tspp_set_system_keys(struct tspp_channel *channel,
1062 struct tspp_system_keys *keys)
1063{
1064 int i;
1065 struct tspp_device *pdev = channel->pdev;
1066
1067 for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
1068 writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
1069
1070 return 0;
1071}
1072
1073static int tspp_channel_init(struct tspp_channel *channel,
1074 struct tspp_device *pdev)
1075{
1076 channel->cdev.owner = THIS_MODULE;
1077 cdev_init(&channel->cdev, &tspp_fops);
1078 channel->pdev = pdev;
1079 channel->data = NULL;
1080 channel->read = NULL;
1081 channel->waiting = NULL;
1082 channel->locked = NULL;
1083 channel->id = MINOR(tspp_minor);
1084 channel->used = 0;
1085 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1086 channel->max_buffers = TSPP_NUM_BUFFERS;
1087 channel->buffer_count = 0;
1088 channel->filter_count = 0;
1089 channel->int_freq = 1;
Liron Kuch72b78552012-10-30 17:47:50 +02001090 channel->src = TSPP_SOURCE_NONE;
1091 channel->mode = TSPP_MODE_DISABLED;
Joel Nider435ad8e2011-12-14 16:53:30 +02001092 channel->notifier = NULL;
1093 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001094 channel->expiration_period_ms = 0;
Liron Kuch72b78552012-10-30 17:47:50 +02001095 channel->memfree = NULL;
1096 channel->user_info = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001097 init_waitqueue_head(&channel->in_queue);
1098
1099 if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
1100 pr_err("tspp: cdev_add failed");
1101 return -EBUSY;
1102 }
1103
1104 channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
1105 channel, "tspp%02d", channel->id);
1106 if (IS_ERR(channel->dd)) {
1107 pr_err("tspp: device_create failed: %i",
1108 (int)PTR_ERR(channel->dd));
1109 cdev_del(&channel->cdev);
1110 return -EBUSY;
1111 }
1112
1113 return 0;
1114}
1115
1116static int tspp_set_buffer_size(struct tspp_channel *channel,
1117 struct tspp_buffer *buf)
1118{
Liron Kuch72b78552012-10-30 17:47:50 +02001119 if (channel->buffer_count > 0) {
1120 pr_err("tspp: cannot set buffer size - buffers already allocated\n");
1121 return -EPERM;
1122 }
1123
Joel Nider435ad8e2011-12-14 16:53:30 +02001124 if (buf->size < TSPP_MIN_BUFFER_SIZE)
1125 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1126 else if (buf->size > TSPP_MAX_BUFFER_SIZE)
1127 channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
1128 else
1129 channel->buffer_size = buf->size;
1130
1131 return 0;
1132}
1133
1134static void tspp_set_tsif_mode(struct tspp_channel *channel,
1135 enum tspp_tsif_mode mode)
1136{
1137 int index;
1138
1139 switch (channel->src) {
1140 case TSPP_SOURCE_TSIF0:
1141 index = 0;
1142 break;
1143 case TSPP_SOURCE_TSIF1:
1144 index = 1;
1145 break;
1146 default:
1147 pr_warn("tspp: can't set mode for non-tsif source %d",
1148 channel->src);
1149 return;
1150 }
1151 channel->pdev->tsif[index].mode = mode;
1152}
1153
Hamad Kadmany92705b32012-10-23 14:15:41 +02001154static void tspp_set_signal_inversion(struct tspp_channel *channel,
Liron Kuch72b78552012-10-30 17:47:50 +02001155 int clock_inverse, int data_inverse,
1156 int sync_inverse, int enable_inverse)
Hamad Kadmany92705b32012-10-23 14:15:41 +02001157{
1158 int index;
1159
1160 switch (channel->src) {
1161 case TSPP_SOURCE_TSIF0:
1162 index = 0;
1163 break;
1164 case TSPP_SOURCE_TSIF1:
1165 index = 1;
1166 break;
1167 default:
1168 return;
1169 }
1170 channel->pdev->tsif[index].clock_inverse = clock_inverse;
1171 channel->pdev->tsif[index].data_inverse = data_inverse;
1172 channel->pdev->tsif[index].sync_inverse = sync_inverse;
1173 channel->pdev->tsif[index].enable_inverse = enable_inverse;
1174}
1175
Liron Kuch72b78552012-10-30 17:47:50 +02001176static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode)
1177{
1178 u32 alignment;
1179
1180 switch (mode) {
1181 case TSPP_MODE_RAW:
1182 /* must be a multiple of 192 */
1183 alignment = (TSPP_PACKET_LENGTH + 4);
1184 if (size % alignment)
1185 return 0;
1186 return 1;
1187
1188 case TSPP_MODE_RAW_NO_SUFFIX:
1189 /* must be a multiple of 188 */
1190 alignment = TSPP_PACKET_LENGTH;
1191 if (size % alignment)
1192 return 0;
1193 return 1;
1194
1195 case TSPP_MODE_DISABLED:
1196 case TSPP_MODE_PES:
1197 default:
1198 /* no alignment requirement */
1199 return 1;
1200 }
1201
1202}
1203
1204static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode)
1205{
1206 u32 new_size;
1207 u32 alignment;
1208
1209 switch (mode) {
1210 case TSPP_MODE_RAW:
1211 /* must be a multiple of 192 */
1212 alignment = (TSPP_PACKET_LENGTH + 4);
1213 break;
1214
1215 case TSPP_MODE_RAW_NO_SUFFIX:
1216 /* must be a multiple of 188 */
1217 alignment = TSPP_PACKET_LENGTH;
1218 break;
1219
1220 case TSPP_MODE_DISABLED:
1221 case TSPP_MODE_PES:
1222 default:
1223 /* no alignment requirement - give the user what he asks for */
1224 alignment = 1;
1225 break;
1226 }
1227 /* align up */
1228 new_size = (((size + alignment - 1) / alignment) * alignment);
1229 return new_size;
1230}
1231
1232static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel)
1233{
1234 int i;
1235 struct tspp_mem_buffer *pbuf, *temp;
1236
1237 pbuf = channel->data;
1238 for (i = 0; i < channel->buffer_count; i++) {
1239 if (pbuf->desc.phys_base) {
1240 if (channel->memfree) {
1241 channel->memfree(channel_id,
1242 pbuf->desc.size,
1243 pbuf->desc.virt_base,
1244 pbuf->desc.phys_base,
1245 channel->user_info);
1246 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +02001247 if (!channel->dma_pool)
1248 dma_free_coherent(NULL,
1249 pbuf->desc.size,
1250 pbuf->desc.virt_base,
1251 pbuf->desc.phys_base);
1252 else
1253 dma_pool_free(channel->dma_pool,
1254 pbuf->desc.virt_base,
1255 pbuf->desc.phys_base);
Liron Kuch72b78552012-10-30 17:47:50 +02001256 }
1257 pbuf->desc.phys_base = 0;
1258 }
1259 pbuf->desc.virt_base = 0;
1260 pbuf->state = TSPP_BUF_STATE_EMPTY;
1261 temp = pbuf;
1262 pbuf = pbuf->next;
1263 kfree(temp);
1264 }
1265}
1266
Joel Nider435ad8e2011-12-14 16:53:30 +02001267/*** TSPP API functions ***/
Liron Kuch72b78552012-10-30 17:47:50 +02001268
1269/**
1270 * tspp_open_stream - open a TSPP stream for use.
1271 *
1272 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1273 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1274 * @source: stream source parameters.
1275 *
1276 * Return error status
1277 *
1278 */
1279int tspp_open_stream(u32 dev, u32 channel_id,
1280 struct tspp_select_source *source)
Joel Nider5556a852011-10-16 10:52:13 +02001281{
1282 u32 val;
1283 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001284 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001285
Hamad Kadmany92705b32012-10-23 14:15:41 +02001286 TSPP_DEBUG("tspp_open_stream %i %i %i %i",
1287 dev, channel_id, source->source, source->mode);
Liron Kuch72b78552012-10-30 17:47:50 +02001288
Joel Nider435ad8e2011-12-14 16:53:30 +02001289 if (dev >= TSPP_MAX_DEVICES) {
1290 pr_err("tspp: device id out of range");
1291 return -ENODEV;
1292 }
Joel Nider5556a852011-10-16 10:52:13 +02001293
Joel Nider435ad8e2011-12-14 16:53:30 +02001294 if (channel_id >= TSPP_NUM_CHANNELS) {
1295 pr_err("tspp: channel id out of range");
1296 return -ECHRNG;
1297 }
1298
1299 pdev = tspp_find_by_id(dev);
1300 if (!pdev) {
1301 pr_err("tspp_str: can't find device %i", dev);
1302 return -ENODEV;
1303 }
1304 channel = &pdev->channels[channel_id];
Hamad Kadmany92705b32012-10-23 14:15:41 +02001305 channel->src = source->source;
1306 tspp_set_tsif_mode(channel, source->mode);
1307 tspp_set_signal_inversion(channel, source->clk_inverse,
Liron Kuch72b78552012-10-30 17:47:50 +02001308 source->data_inverse, source->sync_inverse,
1309 source->enable_inverse);
Joel Nider5556a852011-10-16 10:52:13 +02001310
Hamad Kadmany92705b32012-10-23 14:15:41 +02001311 switch (source->source) {
Joel Nider5556a852011-10-16 10:52:13 +02001312 case TSPP_SOURCE_TSIF0:
Liron Kuch275c0b32013-02-10 15:19:32 +02001313 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1314 pr_err("tspp: error enabling tsif0 GPIOs\n");
1315 return -EBUSY;
1316 }
Joel Nider5556a852011-10-16 10:52:13 +02001317 /* make sure TSIF0 is running & enabled */
1318 if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
1319 pr_err("tspp: error starting tsif0");
Joel Nider435ad8e2011-12-14 16:53:30 +02001320 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001321 }
1322 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1323 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1324 pdev->base + TSPP_CONTROL);
1325 wmb();
1326 break;
1327 case TSPP_SOURCE_TSIF1:
Liron Kuch275c0b32013-02-10 15:19:32 +02001328 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1329 pr_err("tspp: error enabling tsif1 GPIOs\n");
1330 return -EBUSY;
1331 }
Joel Nider5556a852011-10-16 10:52:13 +02001332 /* make sure TSIF1 is running & enabled */
1333 if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
1334 pr_err("tspp: error starting tsif1");
Joel Nider435ad8e2011-12-14 16:53:30 +02001335 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001336 }
1337 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1338 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1339 pdev->base + TSPP_CONTROL);
1340 wmb();
1341 break;
1342 case TSPP_SOURCE_MEM:
1343 break;
1344 default:
Hamad Kadmany92705b32012-10-23 14:15:41 +02001345 pr_err("tspp: channel %i invalid source %i",
1346 channel->id, source->source);
Joel Nider435ad8e2011-12-14 16:53:30 +02001347 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001348 }
1349
Joel Nider5556a852011-10-16 10:52:13 +02001350 return 0;
1351}
1352EXPORT_SYMBOL(tspp_open_stream);
1353
Liron Kuch72b78552012-10-30 17:47:50 +02001354/**
1355 * tspp_close_stream - close a TSPP stream.
1356 *
1357 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1358 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1359 *
1360 * Return error status
1361 *
1362 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001363int tspp_close_stream(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001364{
1365 u32 val;
1366 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001367 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001368
Joel Nider435ad8e2011-12-14 16:53:30 +02001369 if (channel_id >= TSPP_NUM_CHANNELS) {
1370 pr_err("tspp: channel id out of range");
1371 return -ECHRNG;
1372 }
1373 pdev = tspp_find_by_id(dev);
1374 if (!pdev) {
1375 pr_err("tspp_cs: can't find device %i", dev);
1376 return -EBUSY;
1377 }
1378 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001379
1380 switch (channel->src) {
1381 case TSPP_SOURCE_TSIF0:
1382 tspp_stop_tsif(&pdev->tsif[0]);
Liron Kuch275c0b32013-02-10 15:19:32 +02001383 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1384 pr_err("tspp: error disabling tsif0 GPIOs\n");
1385
Joel Nider5556a852011-10-16 10:52:13 +02001386 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1387 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1388 pdev->base + TSPP_CONTROL);
1389 wmb();
1390 break;
1391 case TSPP_SOURCE_TSIF1:
1392 tspp_stop_tsif(&pdev->tsif[1]);
Liron Kuch275c0b32013-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_TSIF1_SRC_DIS,
1398 pdev->base + TSPP_CONTROL);
1399 break;
1400 case TSPP_SOURCE_MEM:
1401 break;
1402 case TSPP_SOURCE_NONE:
1403 break;
1404 }
1405
Joel Nider435ad8e2011-12-14 16:53:30 +02001406 channel->src = TSPP_SOURCE_NONE;
Joel Nider5556a852011-10-16 10:52:13 +02001407 return 0;
1408}
1409EXPORT_SYMBOL(tspp_close_stream);
1410
Liron Kuch72b78552012-10-30 17:47:50 +02001411/**
1412 * tspp_open_channel - open a TSPP channel.
1413 *
1414 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1415 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1416 *
1417 * Return error status
1418 *
1419 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001420int tspp_open_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001421{
1422 int rc = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001423 struct sps_connect *config;
1424 struct sps_register_event *event;
1425 struct tspp_channel *channel;
1426 struct tspp_device *pdev;
1427
1428 if (channel_id >= TSPP_NUM_CHANNELS) {
1429 pr_err("tspp: channel id out of range");
1430 return -ECHRNG;
1431 }
1432 pdev = tspp_find_by_id(dev);
1433 if (!pdev) {
1434 pr_err("tspp_oc: can't find device %i", dev);
1435 return -ENODEV;
1436 }
1437 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001438
1439 if (channel->used) {
1440 pr_err("tspp channel already in use");
Joel Nider435ad8e2011-12-14 16:53:30 +02001441 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001442 }
1443
Joel Nider435ad8e2011-12-14 16:53:30 +02001444 config = &channel->config;
1445 event = &channel->event;
1446
1447 /* start the clocks if needed */
Liron Kuch59339922013-01-01 18:29:47 +02001448 if (tspp_channels_in_use(pdev) == 0) {
1449 tspp_clock_start(pdev);
Joel Nider435ad8e2011-12-14 16:53:30 +02001450 wake_lock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001451 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001452
Joel Nider5556a852011-10-16 10:52:13 +02001453 /* mark it as used */
1454 channel->used = 1;
1455
1456 /* start the bam */
1457 channel->pipe = sps_alloc_endpoint();
1458 if (channel->pipe == 0) {
1459 pr_err("tspp: error allocating endpoint");
1460 rc = -ENOMEM;
1461 goto err_sps_alloc;
1462 }
1463
1464 /* get default configuration */
1465 sps_get_config(channel->pipe, config);
1466
Joel Nider435ad8e2011-12-14 16:53:30 +02001467 config->source = pdev->bam_handle;
Joel Nider5556a852011-10-16 10:52:13 +02001468 config->destination = SPS_DEV_HANDLE_MEM;
1469 config->mode = SPS_MODE_SRC;
Joel Nider435ad8e2011-12-14 16:53:30 +02001470 config->options =
1471 SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
1472 SPS_O_STREAMING | /* streaming mode */
1473 SPS_O_DESC_DONE | /* interrupt on end of descriptor */
Hamad Kadmany81cee052012-11-29 14:15:57 +02001474 SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */
1475 SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */
Joel Nider5556a852011-10-16 10:52:13 +02001476 config->src_pipe_index = channel->id;
1477 config->desc.size =
Hamad Kadmany81cee052012-11-29 14:15:57 +02001478 TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE;
Joel Nider5556a852011-10-16 10:52:13 +02001479 config->desc.base = dma_alloc_coherent(NULL,
1480 config->desc.size,
1481 &config->desc.phys_base,
1482 GFP_KERNEL);
1483 if (config->desc.base == 0) {
1484 pr_err("tspp: error allocating sps descriptors");
1485 rc = -ENOMEM;
1486 goto err_desc_alloc;
1487 }
1488
1489 memset(config->desc.base, 0, config->desc.size);
1490
1491 rc = sps_connect(channel->pipe, config);
1492 if (rc) {
1493 pr_err("tspp: error connecting bam");
1494 goto err_connect;
1495 }
1496
1497 event->mode = SPS_TRIGGER_CALLBACK;
Joel Nider435ad8e2011-12-14 16:53:30 +02001498 event->options = SPS_O_DESC_DONE;
Joel Nider5556a852011-10-16 10:52:13 +02001499 event->callback = tspp_sps_complete_cb;
1500 event->xfer_done = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001501 event->user = pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001502
1503 rc = sps_register_event(channel->pipe, event);
1504 if (rc) {
1505 pr_err("tspp: error registering event");
1506 goto err_event;
1507 }
1508
Hamad Kadmany81cee052012-11-29 14:15:57 +02001509 init_timer(&channel->expiration_timer);
1510 channel->expiration_timer.function = tspp_expiration_timer;
1511 channel->expiration_timer.data = (unsigned long)pdev;
1512 channel->expiration_timer.expires = 0xffffffffL;
1513
Joel Nider435ad8e2011-12-14 16:53:30 +02001514 rc = pm_runtime_get(&pdev->pdev->dev);
Joel Nider5556a852011-10-16 10:52:13 +02001515 if (rc < 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001516 dev_err(&pdev->pdev->dev,
Joel Nider5556a852011-10-16 10:52:13 +02001517 "Runtime PM: Unable to wake up tspp device, rc = %d",
1518 rc);
1519 }
Joel Nider5556a852011-10-16 10:52:13 +02001520 return 0;
1521
1522err_event:
1523 sps_disconnect(channel->pipe);
1524err_connect:
1525 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1526 config->desc.phys_base);
1527err_desc_alloc:
1528 sps_free_endpoint(channel->pipe);
1529err_sps_alloc:
1530 return rc;
1531}
1532EXPORT_SYMBOL(tspp_open_channel);
1533
Liron Kuch72b78552012-10-30 17:47:50 +02001534/**
1535 * tspp_close_channel - close a TSPP channel.
1536 *
1537 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1538 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1539 *
1540 * Return error status
1541 *
1542 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001543int tspp_close_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001544{
1545 int i;
1546 int id;
1547 u32 val;
Joel Nider5556a852011-10-16 10:52:13 +02001548
Joel Nider435ad8e2011-12-14 16:53:30 +02001549 struct sps_connect *config;
1550 struct tspp_device *pdev;
1551 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02001552
1553 if (channel_id >= TSPP_NUM_CHANNELS) {
1554 pr_err("tspp: channel id out of range");
1555 return -ECHRNG;
1556 }
1557 pdev = tspp_find_by_id(dev);
1558 if (!pdev) {
1559 pr_err("tspp_close: can't find device %i", dev);
1560 return -ENODEV;
1561 }
1562 channel = &pdev->channels[channel_id];
1563
1564 /* if the channel is not used, we are done */
1565 if (!channel->used)
1566 return 0;
1567
Hamad Kadmany81cee052012-11-29 14:15:57 +02001568 if (channel->expiration_period_ms)
1569 del_timer(&channel->expiration_timer);
1570
Joel Nider435ad8e2011-12-14 16:53:30 +02001571 channel->notifier = NULL;
1572 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001573 channel->expiration_period_ms = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001574
1575 config = &channel->config;
1576 pdev = channel->pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001577
1578 /* disable pipe (channel) */
1579 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1580 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1581 wmb();
1582
1583 /* unregister all filters for this channel */
1584 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1585 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001586 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001587 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1588 if (id == channel->id) {
1589 if (FILTER_HAS_ENCRYPTION(tspp_filter))
1590 tspp_free_key_entry(
1591 FILTER_GET_KEY_NUMBER(tspp_filter));
1592 tspp_filter->config = 0;
1593 tspp_filter->filter = 0;
1594 }
1595 }
1596 channel->filter_count = 0;
1597
1598 /* stop the stream */
Joel Nider435ad8e2011-12-14 16:53:30 +02001599 tspp_close_stream(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02001600
1601 /* disconnect the bam */
1602 if (sps_disconnect(channel->pipe) != 0)
1603 pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
1604
1605 /* destroy the buffers */
1606 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1607 config->desc.phys_base);
1608
Liron Kuch72b78552012-10-30 17:47:50 +02001609 tspp_destroy_buffers(channel_id, channel);
Hamad Kadmany090709b2013-01-06 12:08:13 +02001610 if (channel->dma_pool) {
1611 dma_pool_destroy(channel->dma_pool);
1612 channel->dma_pool = NULL;
1613 }
Liron Kuch72b78552012-10-30 17:47:50 +02001614
1615 channel->src = TSPP_SOURCE_NONE;
1616 channel->mode = TSPP_MODE_DISABLED;
1617 channel->memfree = NULL;
1618 channel->user_info = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001619 channel->buffer_count = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001620 channel->data = NULL;
1621 channel->read = NULL;
1622 channel->waiting = NULL;
1623 channel->locked = NULL;
1624 channel->used = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001625
Liron Kuch59339922013-01-01 18:29:47 +02001626 if (tspp_channels_in_use(pdev) == 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001627 wake_unlock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001628 tspp_clock_stop(pdev);
1629 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001630
Joel Nider5556a852011-10-16 10:52:13 +02001631 return 0;
1632}
1633EXPORT_SYMBOL(tspp_close_channel);
1634
Liron Kuch72b78552012-10-30 17:47:50 +02001635/**
Hamad Kadmany6d2a9c72013-01-31 14:49:20 +02001636 * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter.
1637 *
1638 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1639 * @source: The TSIF source from which the counter should be read
1640 * @tcr_counter: the value of TCR counter
1641 *
1642 * Return error status
1643 *
1644 * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz.
1645 * If source is neither TSIF 0 or TSIF1 0 is returned.
1646 */
1647int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter)
1648{
1649 struct tspp_device *pdev;
1650 struct tspp_tsif_device *tsif_device;
1651
1652 if (!tcr_counter)
1653 return -EINVAL;
1654
1655 pdev = tspp_find_by_id(dev);
1656 if (!pdev) {
1657 pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev);
1658 return -ENODEV;
1659 }
1660
1661 switch (source) {
1662 case TSPP_SOURCE_TSIF0:
1663 tsif_device = &pdev->tsif[0];
1664 break;
1665
1666 case TSPP_SOURCE_TSIF1:
1667 tsif_device = &pdev->tsif[1];
1668 break;
1669
1670 default:
1671 tsif_device = NULL;
1672 break;
1673 }
1674
1675 if (tsif_device && tsif_device->ref_count)
1676 *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF);
1677 else
1678 *tcr_counter = 0;
1679
1680 return 0;
1681}
1682EXPORT_SYMBOL(tspp_get_ref_clk_counter);
1683
1684/**
Liron Kuch72b78552012-10-30 17:47:50 +02001685 * tspp_add_filter - add a TSPP filter to a channel.
1686 *
1687 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1688 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1689 * @filter: TSPP filter parameters
1690 *
1691 * Return error status
1692 *
1693 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001694int tspp_add_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001695 struct tspp_filter *filter)
1696{
Liron Kuch72b78552012-10-30 17:47:50 +02001697 int i, rc;
Joel Nider5556a852011-10-16 10:52:13 +02001698 int other_channel;
1699 int entry;
1700 u32 val, pid, enabled;
Joel Nider435ad8e2011-12-14 16:53:30 +02001701 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001702 struct tspp_pid_filter p;
Joel Nider435ad8e2011-12-14 16:53:30 +02001703 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001704
Joel Nider435ad8e2011-12-14 16:53:30 +02001705 TSPP_DEBUG("tspp: add filter");
1706 if (channel_id >= TSPP_NUM_CHANNELS) {
1707 pr_err("tspp: channel id out of range");
1708 return -ECHRNG;
1709 }
1710 pdev = tspp_find_by_id(dev);
1711 if (!pdev) {
1712 pr_err("tspp_add: can't find device %i", dev);
1713 return -ENODEV;
1714 }
1715
1716 channel = &pdev->channels[channel_id];
1717
Joel Nider5556a852011-10-16 10:52:13 +02001718 if (filter->source > TSPP_SOURCE_MEM) {
1719 pr_err("tspp invalid source");
Joel Nider435ad8e2011-12-14 16:53:30 +02001720 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001721 }
1722
1723 if (filter->priority >= TSPP_NUM_PRIORITIES) {
1724 pr_err("tspp invalid source");
Joel Nider435ad8e2011-12-14 16:53:30 +02001725 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001726 }
1727
Liron Kuch72b78552012-10-30 17:47:50 +02001728 channel->mode = filter->mode;
1729 /*
1730 * if buffers are already allocated, verify they fulfil
1731 * the alignment requirements.
1732 */
1733 if ((channel->buffer_count > 0) &&
1734 (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode)))
1735 pr_warn("tspp: buffers allocated with incorrect alignment\n");
Joel Nider5556a852011-10-16 10:52:13 +02001736
1737 if (filter->mode == TSPP_MODE_PES) {
1738 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1739 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001740 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001741 pid = FILTER_GET_PIPE_PID((tspp_filter));
1742 enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
1743 if (enabled && (pid == filter->pid)) {
1744 other_channel =
1745 FILTER_GET_PIPE_NUMBER0(tspp_filter);
1746 pr_err("tspp: pid 0x%x already in use by channel %i",
1747 filter->pid, other_channel);
Joel Nider435ad8e2011-12-14 16:53:30 +02001748 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001749 }
1750 }
1751 }
1752
1753 /* make sure this priority is not already in use */
1754 enabled = FILTER_GET_PIPE_PROCESS0(
Joel Nider435ad8e2011-12-14 16:53:30 +02001755 (&(pdev->filters[channel->src]->filter[filter->priority])));
Joel Nider5556a852011-10-16 10:52:13 +02001756 if (enabled) {
1757 pr_err("tspp: filter priority %i source %i is already enabled\n",
1758 filter->priority, channel->src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001759 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001760 }
1761
1762 if (channel->mode == TSPP_MODE_PES) {
1763 /* if we are already processing in PES mode, disable pipe
1764 (channel) and filter to be updated */
1765 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1766 writel_relaxed(val | (1 << channel->id),
1767 pdev->base + TSPP_PS_DISABLE);
1768 wmb();
1769 }
1770
1771 /* update entry */
1772 p.filter = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001773 p.config = FILTER_TRANS_END_DISABLE;
Joel Nider5556a852011-10-16 10:52:13 +02001774 FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
1775 FILTER_SET_PIPE_PID((&p), filter->pid);
1776 FILTER_SET_PID_MASK((&p), filter->mask);
1777 FILTER_SET_PIPE_NUMBER0((&p), channel->id);
1778 FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
1779 if (filter->decrypt) {
1780 entry = tspp_get_key_entry();
1781 if (entry == -1) {
1782 pr_err("tspp: no more keys available!");
1783 } else {
1784 p.config |= FILTER_DECRYPT;
1785 FILTER_SET_KEY_NUMBER((&p), entry);
1786 }
1787 }
Joel Nider5556a852011-10-16 10:52:13 +02001788
Joel Nider435ad8e2011-12-14 16:53:30 +02001789 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001790 filter[filter->priority].config = p.config;
Joel Nider435ad8e2011-12-14 16:53:30 +02001791 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001792 filter[filter->priority].filter = p.filter;
1793
Liron Kuch72b78552012-10-30 17:47:50 +02001794 /*
1795 * allocate buffers if needed (i.e. if user did has not already called
1796 * tspp_allocate_buffers() explicitly).
1797 */
1798 if (channel->buffer_count == 0) {
1799 channel->buffer_size =
Hamad Kadmany090709b2013-01-06 12:08:13 +02001800 tspp_align_buffer_size_by_mode(channel->buffer_size,
Liron Kuch72b78552012-10-30 17:47:50 +02001801 channel->mode);
1802 rc = tspp_allocate_buffers(dev, channel->id,
1803 channel->max_buffers,
1804 channel->buffer_size,
1805 channel->int_freq, NULL, NULL, NULL);
1806 if (rc != 0) {
1807 pr_err("tspp: tspp_allocate_buffers failed\n");
1808 return rc;
1809 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001810 }
1811
Joel Nider5556a852011-10-16 10:52:13 +02001812 /* reenable pipe */
1813 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1814 writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
1815 wmb();
1816 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1817
Joel Nider5556a852011-10-16 10:52:13 +02001818 channel->filter_count++;
1819
1820 return 0;
1821}
1822EXPORT_SYMBOL(tspp_add_filter);
1823
Liron Kuch72b78552012-10-30 17:47:50 +02001824/**
1825 * tspp_remove_filter - remove a TSPP filter from a channel.
1826 *
1827 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1828 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1829 * @filter: TSPP filter parameters
1830 *
1831 * Return error status
1832 *
1833 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001834int tspp_remove_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001835 struct tspp_filter *filter)
1836{
1837 int entry;
1838 u32 val;
Joel Nider435ad8e2011-12-14 16:53:30 +02001839 struct tspp_device *pdev;
1840 int src;
1841 struct tspp_pid_filter *tspp_filter;
1842 struct tspp_channel *channel;
1843
1844 if (channel_id >= TSPP_NUM_CHANNELS) {
1845 pr_err("tspp: channel id out of range");
1846 return -ECHRNG;
1847 }
1848 pdev = tspp_find_by_id(dev);
1849 if (!pdev) {
1850 pr_err("tspp_remove: can't find device %i", dev);
1851 return -ENODEV;
1852 }
1853 channel = &pdev->channels[channel_id];
1854
1855 src = channel->src;
1856 tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
Joel Nider5556a852011-10-16 10:52:13 +02001857
1858 /* disable pipe (channel) */
1859 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1860 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1861 wmb();
1862
1863 /* update data keys */
1864 if (tspp_filter->config & FILTER_DECRYPT) {
1865 entry = FILTER_GET_KEY_NUMBER(tspp_filter);
1866 tspp_free_key_entry(entry);
1867 }
1868
1869 /* update pid table */
1870 tspp_filter->config = 0;
1871 tspp_filter->filter = 0;
1872
1873 channel->filter_count--;
1874
1875 /* reenable pipe */
1876 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1877 writel_relaxed(val & ~(1 << channel->id),
1878 pdev->base + TSPP_PS_DISABLE);
1879 wmb();
1880 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1881
1882 return 0;
1883}
1884EXPORT_SYMBOL(tspp_remove_filter);
1885
Liron Kuch72b78552012-10-30 17:47:50 +02001886/**
1887 * tspp_set_key - set TSPP key in key table.
1888 *
1889 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1890 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1891 * @key: TSPP key parameters
1892 *
1893 * Return error status
1894 *
1895 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001896int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
Joel Nider5556a852011-10-16 10:52:13 +02001897{
1898 int i;
1899 int id;
1900 int key_index;
1901 int data;
Joel Nider435ad8e2011-12-14 16:53:30 +02001902 struct tspp_channel *channel;
1903 struct tspp_device *pdev;
1904
1905 if (channel_id >= TSPP_NUM_CHANNELS) {
1906 pr_err("tspp: channel id out of range");
1907 return -ECHRNG;
1908 }
1909 pdev = tspp_find_by_id(dev);
1910 if (!pdev) {
1911 pr_err("tspp_set: can't find device %i", dev);
1912 return -ENODEV;
1913 }
1914 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001915
1916 /* read the key index used by this channel */
1917 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1918 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001919 &(pdev->filters[channel->src]->filter[i]);
Joel Nider5556a852011-10-16 10:52:13 +02001920 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1921 if (id == channel->id) {
1922 if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
1923 key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
1924 break;
1925 }
1926 }
1927 }
1928 if (i == TSPP_NUM_PRIORITIES) {
1929 pr_err("tspp: no encryption on this channel");
Joel Nider435ad8e2011-12-14 16:53:30 +02001930 return -ENOKEY;
Joel Nider5556a852011-10-16 10:52:13 +02001931 }
1932
1933 if (key->parity == TSPP_KEY_PARITY_EVEN) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001934 pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
1935 pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001936 } else {
Joel Nider435ad8e2011-12-14 16:53:30 +02001937 pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
1938 pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001939 }
1940 data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
1941
1942 return 0;
1943}
1944EXPORT_SYMBOL(tspp_set_key);
1945
Liron Kuch72b78552012-10-30 17:47:50 +02001946/**
1947 * tspp_register_notification - register TSPP channel notification function.
1948 *
1949 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1950 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1951 * @pNotify: notification function
1952 * @userdata: user data to pass to notification function
1953 * @timer_ms: notification for partially filled buffers
1954 *
1955 * Return error status
1956 *
1957 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001958int tspp_register_notification(u32 dev, u32 channel_id,
1959 tspp_notifier *pNotify, void *userdata, u32 timer_ms)
Joel Nider5556a852011-10-16 10:52:13 +02001960{
Joel Nider435ad8e2011-12-14 16:53:30 +02001961 struct tspp_channel *channel;
1962 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001963
Joel Nider435ad8e2011-12-14 16:53:30 +02001964 if (channel_id >= TSPP_NUM_CHANNELS) {
1965 pr_err("tspp: channel id out of range");
1966 return -ECHRNG;
1967 }
1968 pdev = tspp_find_by_id(dev);
1969 if (!pdev) {
1970 pr_err("tspp_reg: can't find device %i", dev);
1971 return -ENODEV;
1972 }
1973 channel = &pdev->channels[channel_id];
1974 channel->notifier = pNotify;
1975 channel->notify_data = userdata;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001976 channel->expiration_period_ms = timer_ms;
1977
Joel Nider5556a852011-10-16 10:52:13 +02001978 return 0;
1979}
Joel Nider435ad8e2011-12-14 16:53:30 +02001980EXPORT_SYMBOL(tspp_register_notification);
Joel Nider5556a852011-10-16 10:52:13 +02001981
Liron Kuch72b78552012-10-30 17:47:50 +02001982/**
1983 * tspp_unregister_notification - unregister TSPP channel notification function.
1984 *
1985 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1986 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1987 *
1988 * Return error status
1989 *
1990 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001991int tspp_unregister_notification(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001992{
Joel Nider435ad8e2011-12-14 16:53:30 +02001993 struct tspp_channel *channel;
1994 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001995
Joel Nider435ad8e2011-12-14 16:53:30 +02001996 if (channel_id >= TSPP_NUM_CHANNELS) {
1997 pr_err("tspp: channel id out of range");
1998 return -ECHRNG;
1999 }
2000 pdev = tspp_find_by_id(dev);
2001 if (!pdev) {
2002 pr_err("tspp_unreg: can't find device %i", dev);
2003 return -ENODEV;
2004 }
2005 channel = &pdev->channels[channel_id];
2006 channel->notifier = NULL;
2007 channel->notify_data = 0;
Joel Nider5556a852011-10-16 10:52:13 +02002008 return 0;
2009}
Joel Nider435ad8e2011-12-14 16:53:30 +02002010EXPORT_SYMBOL(tspp_unregister_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002011
Liron Kuch72b78552012-10-30 17:47:50 +02002012/**
2013 * tspp_get_buffer - get TSPP data buffer.
2014 *
2015 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2016 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2017 *
2018 * Return error status
2019 *
2020 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002021const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002022{
Joel Nider435ad8e2011-12-14 16:53:30 +02002023 struct tspp_mem_buffer *buffer;
2024 struct tspp_channel *channel;
2025 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002026
Joel Nider435ad8e2011-12-14 16:53:30 +02002027 if (channel_id >= TSPP_NUM_CHANNELS) {
2028 pr_err("tspp: channel id out of range");
2029 return NULL;
2030 }
2031 pdev = tspp_find_by_id(dev);
2032 if (!pdev) {
2033 pr_err("tspp_get: can't find device %i", dev);
2034 return NULL;
2035 }
2036 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02002037
Joel Nider435ad8e2011-12-14 16:53:30 +02002038 if (!channel->read) {
2039 pr_warn("tspp: no buffer to get on channel %i!",
2040 channel->id);
2041 return NULL;
2042 }
2043
2044 buffer = channel->read;
2045 /* see if we have any buffers ready to read */
2046 if (buffer->state != TSPP_BUF_STATE_DATA)
2047 return 0;
2048
2049 if (buffer->state == TSPP_BUF_STATE_DATA) {
2050 /* mark the buffer as busy */
2051 buffer->state = TSPP_BUF_STATE_LOCKED;
2052
2053 /* increment the pointer along the list */
2054 channel->read = channel->read->next;
2055 }
2056
2057 return &buffer->desc;
2058}
2059EXPORT_SYMBOL(tspp_get_buffer);
2060
Liron Kuch72b78552012-10-30 17:47:50 +02002061/**
2062 * tspp_release_buffer - release TSPP data buffer back to TSPP.
2063 *
2064 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2065 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2066 * @descriptor_id: buffer descriptor ID
2067 *
2068 * Return error status
2069 *
2070 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002071int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
2072{
2073 int i, found = 0;
2074 struct tspp_mem_buffer *buffer;
2075 struct tspp_channel *channel;
2076 struct tspp_device *pdev;
2077
2078 if (channel_id >= TSPP_NUM_CHANNELS) {
2079 pr_err("tspp: channel id out of range");
2080 return -ECHRNG;
2081 }
2082 pdev = tspp_find_by_id(dev);
2083 if (!pdev) {
2084 pr_err("tspp: can't find device %i", dev);
2085 return -ENODEV;
2086 }
2087 channel = &pdev->channels[channel_id];
2088
2089 if (descriptor_id > channel->buffer_count)
2090 pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
2091
2092 /* find the correct descriptor */
2093 buffer = channel->locked;
2094 for (i = 0; i < channel->buffer_count; i++) {
2095 if (buffer->desc.id == descriptor_id) {
2096 found = 1;
2097 break;
2098 }
2099 buffer = buffer->next;
2100 }
2101 channel->locked = channel->locked->next;
2102
2103 if (!found) {
2104 pr_err("tspp: cant find desc %i", descriptor_id);
2105 return -EINVAL;
2106 }
2107
2108 /* make sure the buffer is in the expected state */
2109 if (buffer->state != TSPP_BUF_STATE_LOCKED) {
2110 pr_err("tspp: buffer %i not locked", descriptor_id);
2111 return -EINVAL;
2112 }
2113 /* unlock the buffer and requeue it */
2114 buffer->state = TSPP_BUF_STATE_WAITING;
2115
2116 if (tspp_queue_buffer(channel, buffer))
2117 pr_warn("tspp: can't requeue buffer");
Joel Nider5556a852011-10-16 10:52:13 +02002118 return 0;
2119}
Joel Nider435ad8e2011-12-14 16:53:30 +02002120EXPORT_SYMBOL(tspp_release_buffer);
2121
Liron Kuch72b78552012-10-30 17:47:50 +02002122/**
2123 * tspp_allocate_buffers - allocate TSPP data buffers.
2124 *
2125 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2126 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2127 * @count: number of buffers to allocate
2128 * @size: size of each buffer to allocate
2129 * @int_freq: interrupt frequency
2130 * @alloc: user defined memory allocator function. Pass NULL for default.
2131 * @memfree: user defined memory free function. Pass NULL for default.
2132 * @user: user data to pass to the memory allocator/free function
2133 *
2134 * Return error status
2135 *
2136 * The user can optionally call this function explicitly to allocate the TSPP
2137 * data buffers. Alternatively, if the user did not call this function, it
2138 * is called implicitly by tspp_add_filter().
2139 */
2140int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size,
2141 u32 int_freq, tspp_allocator *alloc,
2142 tspp_memfree *memfree, void *user)
Joel Nider435ad8e2011-12-14 16:53:30 +02002143{
2144 struct tspp_channel *channel;
2145 struct tspp_device *pdev;
2146 struct tspp_mem_buffer *last = NULL;
2147
2148 TSPP_DEBUG("tspp_allocate_buffers");
2149
2150 if (channel_id >= TSPP_NUM_CHANNELS) {
Liron Kuch72b78552012-10-30 17:47:50 +02002151 pr_err("%s: channel id out of range", __func__);
Joel Nider435ad8e2011-12-14 16:53:30 +02002152 return -ECHRNG;
2153 }
Liron Kuch72b78552012-10-30 17:47:50 +02002154
Joel Nider435ad8e2011-12-14 16:53:30 +02002155 pdev = tspp_find_by_id(dev);
2156 if (!pdev) {
Liron Kuch72b78552012-10-30 17:47:50 +02002157 pr_err("%s: can't find device %i", __func__, dev);
Joel Nider435ad8e2011-12-14 16:53:30 +02002158 return -ENODEV;
2159 }
Liron Kuch72b78552012-10-30 17:47:50 +02002160
2161 if (count < MIN_ACCEPTABLE_BUFFER_COUNT) {
2162 pr_err("%s: tspp requires a minimum of %i buffers\n",
2163 __func__, MIN_ACCEPTABLE_BUFFER_COUNT);
2164 return -EINVAL;
2165 }
2166
Joel Nider435ad8e2011-12-14 16:53:30 +02002167 channel = &pdev->channels[channel_id];
Hamad Kadmany090709b2013-01-06 12:08:13 +02002168
Liron Kuch72b78552012-10-30 17:47:50 +02002169 /* allow buffer allocation only if there was no previous buffer
2170 * allocation for this channel.
2171 */
2172 if (channel->buffer_count > 0) {
2173 pr_err("%s: buffers already allocated for channel %u",
2174 __func__, channel_id);
2175 return -EINVAL;
2176 }
Joel Nider435ad8e2011-12-14 16:53:30 +02002177
2178 channel->max_buffers = count;
2179
2180 /* set up interrupt frequency */
Liron Kuch72b78552012-10-30 17:47:50 +02002181 if (int_freq > channel->max_buffers) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002182 int_freq = channel->max_buffers;
Liron Kuch72b78552012-10-30 17:47:50 +02002183 pr_warn("%s: setting interrupt frequency to %u\n",
2184 __func__, int_freq);
Joel Nider435ad8e2011-12-14 16:53:30 +02002185 }
Liron Kuch72b78552012-10-30 17:47:50 +02002186 channel->int_freq = int_freq;
2187 /*
2188 * it is the responsibility of the caller to tspp_allocate_buffers(),
2189 * whether it's the user or the driver, to make sure the size parameter
2190 * is compatible to the channel mode.
2191 */
2192 channel->buffer_size = size;
Joel Nider435ad8e2011-12-14 16:53:30 +02002193
Liron Kuch72b78552012-10-30 17:47:50 +02002194 /* save user defined memory free function for later use */
2195 channel->memfree = memfree;
2196 channel->user_info = user;
2197
Hamad Kadmany090709b2013-01-06 12:08:13 +02002198 /*
2199 * For small buffers, create a DMA pool so that memory
2200 * is not wasted through dma_alloc_coherent.
2201 */
2202 if (TSPP_USE_DMA_POOL(channel->buffer_size)) {
2203 channel->dma_pool = dma_pool_create("tspp",
2204 NULL, channel->buffer_size, 0, 0);
2205 if (!channel->dma_pool) {
2206 pr_err("%s: Can't allocate memory pool\n", __func__);
2207 return -ENOMEM;
2208 }
2209 } else {
2210 channel->dma_pool = NULL;
2211 }
2212
2213
Liron Kuch72b78552012-10-30 17:47:50 +02002214 for (channel->buffer_count = 0;
2215 channel->buffer_count < channel->max_buffers;
Joel Nider435ad8e2011-12-14 16:53:30 +02002216 channel->buffer_count++) {
2217
2218 /* allocate the descriptor */
2219 struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
2220 kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
2221 if (!desc) {
Liron Kuch72b78552012-10-30 17:47:50 +02002222 pr_warn("%s: Can't allocate desc %i",
2223 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002224 break;
2225 }
2226
2227 desc->desc.id = channel->buffer_count;
2228 /* allocate the buffer */
2229 if (tspp_alloc_buffer(channel_id, &desc->desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +02002230 channel->buffer_size, channel->dma_pool,
2231 alloc, user) != 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002232 kfree(desc);
Liron Kuch72b78552012-10-30 17:47:50 +02002233 pr_warn("%s: Can't allocate buffer %i",
2234 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002235 break;
2236 }
2237
2238 /* add the descriptor to the list */
2239 desc->filled = 0;
2240 desc->read_index = 0;
2241 if (!channel->data) {
2242 channel->data = desc;
2243 desc->next = channel->data;
2244 } else {
2245 last->next = desc;
2246 }
2247 last = desc;
2248 desc->next = channel->data;
2249
2250 /* prepare the sps descriptor */
2251 desc->sps.phys_base = desc->desc.phys_base;
2252 desc->sps.base = desc->desc.virt_base;
2253 desc->sps.size = desc->desc.size;
2254
2255 /* start the transfer */
2256 if (tspp_queue_buffer(channel, desc))
Liron Kuch72b78552012-10-30 17:47:50 +02002257 pr_err("%s: can't queue buffer %i",
2258 __func__, desc->desc.id);
2259 }
2260
2261 if (channel->buffer_count < channel->max_buffers) {
2262 /*
2263 * we failed to allocate the requested number of buffers.
2264 * we don't allow a partial success, so need to clean up here.
2265 */
2266 tspp_destroy_buffers(channel_id, channel);
2267 channel->buffer_count = 0;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002268
2269 if (channel->dma_pool) {
2270 dma_pool_destroy(channel->dma_pool);
2271 channel->dma_pool = NULL;
2272 }
Liron Kuch72b78552012-10-30 17:47:50 +02002273 return -ENOMEM;
Joel Nider435ad8e2011-12-14 16:53:30 +02002274 }
2275
2276 channel->waiting = channel->data;
2277 channel->read = channel->data;
2278 channel->locked = channel->data;
Liron Kuch72b78552012-10-30 17:47:50 +02002279
Hamad Kadmany81cee052012-11-29 14:15:57 +02002280 /* Now that buffers are scheduled to HW, kick data expiration timer */
2281 if (channel->expiration_period_ms)
2282 mod_timer(&channel->expiration_timer,
2283 jiffies +
2284 MSEC_TO_JIFFIES(
2285 channel->expiration_period_ms));
2286
Joel Nider435ad8e2011-12-14 16:53:30 +02002287 return 0;
2288}
2289EXPORT_SYMBOL(tspp_allocate_buffers);
Joel Nider5556a852011-10-16 10:52:13 +02002290
2291/*** File Operations ***/
2292static ssize_t tspp_open(struct inode *inode, struct file *filp)
2293{
Joel Nider435ad8e2011-12-14 16:53:30 +02002294 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002295 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002296
2297 TSPP_DEBUG("tspp_open");
Joel Nider5556a852011-10-16 10:52:13 +02002298 channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
2299 filp->private_data = channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002300 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002301
2302 /* if this channel is already in use, quit */
2303 if (channel->used) {
2304 pr_err("tspp channel %i already in use",
2305 MINOR(channel->cdev.dev));
2306 return -EACCES;
2307 }
2308
Joel Nider435ad8e2011-12-14 16:53:30 +02002309 if (tspp_open_channel(dev, channel->id) != 0) {
Joel Nider5556a852011-10-16 10:52:13 +02002310 pr_err("tspp: error opening channel");
2311 return -EACCES;
2312 }
2313
2314 return 0;
2315}
2316
2317static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
2318{
2319 unsigned long flags;
2320 unsigned int mask = 0;
2321 struct tspp_channel *channel;
2322 channel = filp->private_data;
2323
2324 /* register the wait queue for this channel */
2325 poll_wait(filp, &channel->in_queue, p);
2326
2327 spin_lock_irqsave(&channel->pdev->spinlock, flags);
Joel Nider435ad8e2011-12-14 16:53:30 +02002328 if (channel->read &&
2329 channel->read->state == TSPP_BUF_STATE_DATA)
Joel Nider5556a852011-10-16 10:52:13 +02002330 mask = POLLIN | POLLRDNORM;
2331
2332 spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
2333
2334 return mask;
2335}
2336
2337static ssize_t tspp_release(struct inode *inode, struct file *filp)
2338{
Joel Nider435ad8e2011-12-14 16:53:30 +02002339 struct tspp_channel *channel = filp->private_data;
2340 u32 dev = channel->pdev->pdev->id;
2341 TSPP_DEBUG("tspp_release");
Joel Nider5556a852011-10-16 10:52:13 +02002342
Joel Nider435ad8e2011-12-14 16:53:30 +02002343 tspp_close_channel(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02002344
2345 return 0;
2346}
2347
2348static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
2349 loff_t *f_pos)
2350{
2351 size_t size = 0;
2352 size_t transferred = 0;
2353 struct tspp_channel *channel;
2354 struct tspp_mem_buffer *buffer;
2355 channel = filp->private_data;
2356
2357 TSPP_DEBUG("tspp_read");
Joel Nider435ad8e2011-12-14 16:53:30 +02002358
2359 while (!channel->read) {
2360 if (filp->f_flags & O_NONBLOCK) {
2361 pr_warn("tspp: no buffer on channel %i!",
2362 channel->id);
2363 return -EAGAIN;
2364 }
2365 /* go to sleep if there is nothing to read */
2366 if (wait_event_interruptible(channel->in_queue,
2367 (channel->read != NULL))) {
2368 pr_err("tspp: rude awakening\n");
2369 return -ERESTARTSYS;
2370 }
2371 }
2372
2373 buffer = channel->read;
2374
Joel Nider5556a852011-10-16 10:52:13 +02002375 /* see if we have any buffers ready to read */
2376 while (buffer->state != TSPP_BUF_STATE_DATA) {
2377 if (filp->f_flags & O_NONBLOCK) {
2378 pr_warn("tspp: nothing to read on channel %i!",
2379 channel->id);
2380 return -EAGAIN;
2381 }
2382 /* go to sleep if there is nothing to read */
Joel Nider5556a852011-10-16 10:52:13 +02002383 if (wait_event_interruptible(channel->in_queue,
2384 (buffer->state == TSPP_BUF_STATE_DATA))) {
2385 pr_err("tspp: rude awakening\n");
2386 return -ERESTARTSYS;
2387 }
2388 }
2389
2390 while (buffer->state == TSPP_BUF_STATE_DATA) {
2391 size = min(count, buffer->filled);
Joel Nider5556a852011-10-16 10:52:13 +02002392 if (size == 0)
2393 break;
2394
Joel Nider435ad8e2011-12-14 16:53:30 +02002395 if (copy_to_user(buf, buffer->desc.virt_base +
Joel Nider5556a852011-10-16 10:52:13 +02002396 buffer->read_index, size)) {
2397 pr_err("tspp: error copying to user buffer");
Joel Nider435ad8e2011-12-14 16:53:30 +02002398 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02002399 }
2400 buf += size;
2401 count -= size;
2402 transferred += size;
2403 buffer->read_index += size;
2404
Liron Kuch72b78552012-10-30 17:47:50 +02002405 /*
2406 * after reading the end of the buffer, requeue it,
2407 * and set up for reading the next one
2408 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002409 if (buffer->read_index == buffer->filled) {
Joel Nider5556a852011-10-16 10:52:13 +02002410 buffer->state = TSPP_BUF_STATE_WAITING;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002411
Joel Nider435ad8e2011-12-14 16:53:30 +02002412 if (tspp_queue_buffer(channel, buffer))
2413 pr_err("tspp: can't submit transfer");
Hamad Kadmany090709b2013-01-06 12:08:13 +02002414
Joel Nider435ad8e2011-12-14 16:53:30 +02002415 channel->locked = channel->read;
2416 channel->read = channel->read->next;
Joel Nider5556a852011-10-16 10:52:13 +02002417 }
2418 }
2419
2420 return transferred;
2421}
2422
2423static long tspp_ioctl(struct file *filp,
2424 unsigned int param0, unsigned long param1)
2425{
Joel Nider435ad8e2011-12-14 16:53:30 +02002426 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002427 int rc = -1;
2428 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002429 struct tspp_select_source ss;
2430 struct tspp_filter f;
2431 struct tspp_key k;
2432 struct tspp_iv iv;
2433 struct tspp_system_keys sk;
2434 struct tspp_buffer b;
Joel Nider5556a852011-10-16 10:52:13 +02002435 channel = filp->private_data;
Joel Nider435ad8e2011-12-14 16:53:30 +02002436 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002437
2438 if (!param1)
2439 return -EINVAL;
2440
2441 switch (param0) {
2442 case TSPP_IOCTL_SELECT_SOURCE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002443 if (!access_ok(VERIFY_READ, param1,
2444 sizeof(struct tspp_select_source))) {
2445 return -EBUSY;
2446 }
2447 if (__copy_from_user(&ss, (void *)param1,
2448 sizeof(struct tspp_select_source)) == 0)
2449 rc = tspp_select_source(dev, channel->id, &ss);
Joel Nider5556a852011-10-16 10:52:13 +02002450 break;
2451 case TSPP_IOCTL_ADD_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002452 if (!access_ok(VERIFY_READ, param1,
2453 sizeof(struct tspp_filter))) {
2454 return -ENOSR;
2455 }
2456 if (__copy_from_user(&f, (void *)param1,
2457 sizeof(struct tspp_filter)) == 0)
2458 rc = tspp_add_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002459 break;
2460 case TSPP_IOCTL_REMOVE_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002461 if (!access_ok(VERIFY_READ, param1,
2462 sizeof(struct tspp_filter))) {
2463 return -EBUSY;
2464 }
2465 if (__copy_from_user(&f, (void *)param1,
2466 sizeof(struct tspp_filter)) == 0)
2467 rc = tspp_remove_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002468 break;
2469 case TSPP_IOCTL_SET_KEY:
Joel Nider435ad8e2011-12-14 16:53:30 +02002470 if (!access_ok(VERIFY_READ, param1,
2471 sizeof(struct tspp_key))) {
2472 return -EBUSY;
2473 }
2474 if (__copy_from_user(&k, (void *)param1,
2475 sizeof(struct tspp_key)) == 0)
2476 rc = tspp_set_key(dev, channel->id, &k);
Joel Nider5556a852011-10-16 10:52:13 +02002477 break;
2478 case TSPP_IOCTL_SET_IV:
Joel Nider435ad8e2011-12-14 16:53:30 +02002479 if (!access_ok(VERIFY_READ, param1,
2480 sizeof(struct tspp_iv))) {
2481 return -EBUSY;
2482 }
2483 if (__copy_from_user(&iv, (void *)param1,
2484 sizeof(struct tspp_iv)) == 0)
2485 rc = tspp_set_iv(channel, &iv);
Joel Nider5556a852011-10-16 10:52:13 +02002486 break;
2487 case TSPP_IOCTL_SET_SYSTEM_KEYS:
Joel Nider435ad8e2011-12-14 16:53:30 +02002488 if (!access_ok(VERIFY_READ, param1,
2489 sizeof(struct tspp_system_keys))) {
2490 return -EINVAL;
2491 }
2492 if (__copy_from_user(&sk, (void *)param1,
2493 sizeof(struct tspp_system_keys)) == 0)
2494 rc = tspp_set_system_keys(channel, &sk);
Joel Nider5556a852011-10-16 10:52:13 +02002495 break;
2496 case TSPP_IOCTL_BUFFER_SIZE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002497 if (!access_ok(VERIFY_READ, param1,
2498 sizeof(struct tspp_buffer))) {
2499 rc = -EINVAL;
2500 }
2501 if (__copy_from_user(&b, (void *)param1,
2502 sizeof(struct tspp_buffer)) == 0)
2503 rc = tspp_set_buffer_size(channel, &b);
Joel Nider5556a852011-10-16 10:52:13 +02002504 break;
2505 default:
2506 pr_err("tspp: Unknown ioctl %i", param0);
2507 }
2508
Liron Kuch72b78552012-10-30 17:47:50 +02002509 /*
2510 * normalize the return code in case one of the subfunctions does
2511 * something weird
2512 */
Joel Nider5556a852011-10-16 10:52:13 +02002513 if (rc != 0)
Joel Nider435ad8e2011-12-14 16:53:30 +02002514 rc = -ENOIOCTLCMD;
Joel Nider5556a852011-10-16 10:52:13 +02002515
2516 return rc;
2517}
2518
2519/*** debugfs ***/
Joel Nider5556a852011-10-16 10:52:13 +02002520static int debugfs_iomem_x32_set(void *data, u64 val)
2521{
2522 writel_relaxed(val, data);
2523 wmb();
2524 return 0;
2525}
2526
2527static int debugfs_iomem_x32_get(void *data, u64 *val)
2528{
2529 *val = readl_relaxed(data);
2530 return 0;
2531}
2532
2533DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
2534 debugfs_iomem_x32_set, "0x%08llx");
2535
2536static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
2537 int instance)
2538{
2539 char name[10];
2540 snprintf(name, 10, "tsif%i", instance);
2541 tsif_device->dent_tsif = debugfs_create_dir(
2542 name, NULL);
2543 if (tsif_device->dent_tsif) {
2544 int i;
2545 void __iomem *base = tsif_device->base;
2546 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
2547 tsif_device->debugfs_tsif_regs[i] =
2548 debugfs_create_file(
2549 debugfs_tsif_regs[i].name,
2550 debugfs_tsif_regs[i].mode,
2551 tsif_device->dent_tsif,
2552 base + debugfs_tsif_regs[i].offset,
2553 &fops_iomem_x32);
2554 }
Hamad Kadmany44307d32012-11-25 09:49:51 +02002555
2556 debugfs_create_u32(
2557 "stat_rx_chunks",
2558 S_IRUGO|S_IWUGO,
2559 tsif_device->dent_tsif,
2560 &tsif_device->stat_rx);
2561
2562 debugfs_create_u32(
2563 "stat_overflow",
2564 S_IRUGO|S_IWUGO,
2565 tsif_device->dent_tsif,
2566 &tsif_device->stat_overflow);
2567
2568 debugfs_create_u32(
2569 "stat_lost_sync",
2570 S_IRUGO|S_IWUGO,
2571 tsif_device->dent_tsif,
2572 &tsif_device->stat_lost_sync);
2573
2574 debugfs_create_u32(
2575 "stat_timeout",
2576 S_IRUGO|S_IWUGO,
2577 tsif_device->dent_tsif,
2578 &tsif_device->stat_timeout);
2579
Joel Nider5556a852011-10-16 10:52:13 +02002580 }
2581}
2582
2583static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
2584{
2585 if (tsif_device->dent_tsif) {
2586 int i;
2587 debugfs_remove_recursive(tsif_device->dent_tsif);
2588 tsif_device->dent_tsif = NULL;
2589 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
2590 tsif_device->debugfs_tsif_regs[i] = NULL;
2591 }
2592}
2593
2594static void tspp_debugfs_init(struct tspp_device *device, int instance)
2595{
2596 char name[10];
2597 snprintf(name, 10, "tspp%i", instance);
2598 device->dent = debugfs_create_dir(
2599 name, NULL);
2600 if (device->dent) {
2601 int i;
2602 void __iomem *base = device->base;
2603 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
2604 device->debugfs_regs[i] =
2605 debugfs_create_file(
2606 debugfs_tspp_regs[i].name,
2607 debugfs_tspp_regs[i].mode,
2608 device->dent,
2609 base + debugfs_tspp_regs[i].offset,
2610 &fops_iomem_x32);
2611 }
2612 }
2613}
2614
2615static void tspp_debugfs_exit(struct tspp_device *device)
2616{
2617 if (device->dent) {
2618 int i;
2619 debugfs_remove_recursive(device->dent);
2620 device->dent = NULL;
2621 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
2622 device->debugfs_regs[i] = NULL;
2623 }
2624}
Joel Nider5556a852011-10-16 10:52:13 +02002625
Liron Kuch59339922013-01-01 18:29:47 +02002626/* copy device-tree data to platfrom data struct */
2627static __devinit struct msm_tspp_platform_data *
2628msm_tspp_dt_to_pdata(struct platform_device *pdev)
2629{
2630 struct device_node *node = pdev->dev.of_node;
2631 struct msm_tspp_platform_data *data;
2632 struct msm_gpio *gpios;
2633 int i, rc;
2634 int gpio;
2635 u32 gpio_func;
2636
2637 /* Note: memory allocated by devm_kzalloc is freed automatically */
2638 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2639 if (!data) {
2640 pr_err("tspp: Unable to allocate platform data\n");
2641 return NULL;
2642 }
2643 rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk);
2644 if (rc) {
2645 pr_err("tspp: Could not find tsif-pclk property, err = %d\n",
2646 rc);
2647 return NULL;
2648 }
2649 rc = of_property_read_string(node, "qcom,tsif-ref-clk",
2650 &data->tsif_ref_clk);
2651 if (rc) {
2652 pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n",
2653 rc);
2654 return NULL;
2655 }
2656
2657 data->num_gpios = of_gpio_count(node);
2658 if (data->num_gpios == 0) {
2659 pr_err("tspp: Could not find GPIO definitions\n");
2660 return NULL;
2661 }
2662 gpios = devm_kzalloc(&pdev->dev,
2663 (data->num_gpios * sizeof(struct msm_gpio)),
2664 GFP_KERNEL);
2665 if (!gpios) {
2666 pr_err("tspp: Unable to allocate memory for GPIOs table\n");
2667 return NULL;
2668 }
2669 /* Assuming GPIO FUNC property is the same for all GPIOs */
2670 if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) {
2671 pr_err("tspp: Could not find gpios-func property\n");
2672 return NULL;
2673 }
2674 for (i = 0; i < data->num_gpios; i++) {
2675 gpio = of_get_gpio(node, i);
2676 gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func,
2677 GPIO_CFG_INPUT,
2678 GPIO_CFG_PULL_DOWN,
2679 GPIO_CFG_2MA);
2680 rc = of_property_read_string_index(node, "qcom,gpio-names",
2681 i, &gpios[i].label);
2682 if (rc)
2683 pr_warn("tspp: Could not find gpio-names property\n");
2684 }
2685
2686 data->gpios = gpios;
2687
2688 return data;
2689}
2690
2691static int msm_tspp_map_irqs(struct platform_device *pdev,
2692 struct tspp_device *device)
2693{
2694 int rc;
2695 int i;
2696
2697 /* get IRQ numbers from platform information */
2698
2699 /* map TSPP IRQ */
2700 rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ");
2701 if (rc > 0) {
2702 device->tspp_irq = rc;
2703 rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
2704 dev_name(&pdev->dev), device);
2705 if (rc) {
2706 dev_err(&pdev->dev,
2707 "failed to request TSPP IRQ %d : %d",
2708 device->tspp_irq, rc);
2709 device->tspp_irq = 0;
2710 return -EINVAL;
2711 }
2712 } else {
2713 dev_err(&pdev->dev, "failed to get TSPP IRQ");
2714 return -EINVAL;
2715 }
2716
2717 /* map TSIF IRQs */
2718 rc = platform_get_irq_byname(pdev, "TSIF0_IRQ");
2719 if (rc > 0) {
2720 device->tsif[0].tsif_irq = rc;
2721 } else {
2722 dev_err(&pdev->dev, "failed to get TSIF0 IRQ");
2723 return -EINVAL;
2724 }
2725
2726 rc = platform_get_irq_byname(pdev, "TSIF1_IRQ");
2727 if (rc > 0) {
2728 device->tsif[1].tsif_irq = rc;
2729 } else {
2730 dev_err(&pdev->dev, "failed to get TSIF1 IRQ");
2731 return -EINVAL;
2732 }
2733
2734 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2735 rc = request_irq(device->tsif[i].tsif_irq,
2736 tsif_isr, IRQF_SHARED,
2737 dev_name(&pdev->dev), &device->tsif[i]);
2738 if (rc) {
2739 dev_warn(&pdev->dev, "failed to request TSIF%d IRQ: %d",
2740 i, rc);
2741 device->tsif[i].tsif_irq = 0;
2742 }
2743 }
2744
2745 /* map BAM IRQ */
2746 rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ");
2747 if (rc > 0) {
2748 device->bam_irq = rc;
2749 } else {
2750 dev_err(&pdev->dev, "failed to get TSPP BAM IRQ");
2751 return -EINVAL;
2752 }
2753
2754 return 0;
2755}
2756
Joel Nider5556a852011-10-16 10:52:13 +02002757static int __devinit msm_tspp_probe(struct platform_device *pdev)
2758{
2759 int rc = -ENODEV;
2760 u32 version;
Liron Kuch72b78552012-10-30 17:47:50 +02002761 u32 i, j;
Joel Nider5556a852011-10-16 10:52:13 +02002762 struct msm_tspp_platform_data *data;
2763 struct tspp_device *device;
2764 struct resource *mem_tsif0;
2765 struct resource *mem_tsif1;
2766 struct resource *mem_tspp;
2767 struct resource *mem_bam;
Liron Kuch72b78552012-10-30 17:47:50 +02002768 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02002769
Liron Kuch59339922013-01-01 18:29:47 +02002770 if (pdev->dev.of_node) {
2771 /* get information from device tree */
2772 data = msm_tspp_dt_to_pdata(pdev);
2773 /* get device ID */
2774 rc = of_property_read_u32(pdev->dev.of_node,
2775 "cell-index", &pdev->id);
2776 if (rc)
2777 pdev->id = -1;
2778
2779 pdev->dev.platform_data = data;
2780 } else {
2781 /* must have platform data */
2782 data = pdev->dev.platform_data;
2783 }
Joel Nider5556a852011-10-16 10:52:13 +02002784 if (!data) {
2785 pr_err("tspp: Platform data not available");
2786 rc = -EINVAL;
2787 goto out;
2788 }
2789
2790 /* check for valid device id */
Joel Nider435ad8e2011-12-14 16:53:30 +02002791 if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
Joel Nider5556a852011-10-16 10:52:13 +02002792 pr_err("tspp: Invalid device ID %d", pdev->id);
2793 rc = -EINVAL;
2794 goto out;
2795 }
2796
2797 /* OK, we will use this device */
2798 device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
2799 if (!device) {
2800 pr_err("tspp: Failed to allocate memory for device");
2801 rc = -ENOMEM;
2802 goto out;
2803 }
2804
2805 /* set up references */
2806 device->pdev = pdev;
2807 platform_set_drvdata(pdev, device);
2808
2809 /* map clocks */
2810 if (data->tsif_pclk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002811 device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
Joel Nider5556a852011-10-16 10:52:13 +02002812 if (IS_ERR(device->tsif_pclk)) {
2813 pr_err("tspp: failed to get %s",
2814 data->tsif_pclk);
2815 rc = PTR_ERR(device->tsif_pclk);
2816 device->tsif_pclk = NULL;
2817 goto err_pclock;
2818 }
2819 }
2820 if (data->tsif_ref_clk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002821 device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
Joel Nider5556a852011-10-16 10:52:13 +02002822 if (IS_ERR(device->tsif_ref_clk)) {
2823 pr_err("tspp: failed to get %s",
2824 data->tsif_ref_clk);
2825 rc = PTR_ERR(device->tsif_ref_clk);
2826 device->tsif_ref_clk = NULL;
2827 goto err_refclock;
2828 }
2829 }
2830
2831 /* map I/O memory */
Liron Kuch59339922013-01-01 18:29:47 +02002832 mem_tsif0 = platform_get_resource_byname(pdev,
2833 IORESOURCE_MEM, "MSM_TSIF0_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002834 if (!mem_tsif0) {
2835 pr_err("tspp: Missing tsif0 MEM resource");
2836 rc = -ENXIO;
2837 goto err_res_tsif0;
2838 }
2839 device->tsif[0].base = ioremap(mem_tsif0->start,
2840 resource_size(mem_tsif0));
2841 if (!device->tsif[0].base) {
2842 pr_err("tspp: ioremap failed");
2843 goto err_map_tsif0;
2844 }
2845
Liron Kuch59339922013-01-01 18:29:47 +02002846 mem_tsif1 = platform_get_resource_byname(pdev,
2847 IORESOURCE_MEM, "MSM_TSIF1_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002848 if (!mem_tsif1) {
2849 dev_err(&pdev->dev, "Missing tsif1 MEM resource");
2850 rc = -ENXIO;
2851 goto err_res_tsif1;
2852 }
2853 device->tsif[1].base = ioremap(mem_tsif1->start,
2854 resource_size(mem_tsif1));
2855 if (!device->tsif[1].base) {
2856 dev_err(&pdev->dev, "ioremap failed");
2857 goto err_map_tsif1;
2858 }
2859
Liron Kuch59339922013-01-01 18:29:47 +02002860 mem_tspp = platform_get_resource_byname(pdev,
2861 IORESOURCE_MEM, "MSM_TSPP_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002862 if (!mem_tspp) {
2863 dev_err(&pdev->dev, "Missing MEM resource");
2864 rc = -ENXIO;
2865 goto err_res_dev;
2866 }
2867 device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
2868 if (!device->base) {
2869 dev_err(&pdev->dev, "ioremap failed");
2870 goto err_map_dev;
2871 }
2872
Liron Kuch59339922013-01-01 18:29:47 +02002873 mem_bam = platform_get_resource_byname(pdev,
2874 IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002875 if (!mem_bam) {
2876 pr_err("tspp: Missing bam MEM resource");
2877 rc = -ENXIO;
2878 goto err_res_bam;
2879 }
2880 memset(&device->bam_props, 0, sizeof(device->bam_props));
2881 device->bam_props.phys_addr = mem_bam->start;
2882 device->bam_props.virt_addr = ioremap(mem_bam->start,
2883 resource_size(mem_bam));
2884 if (!device->bam_props.virt_addr) {
2885 dev_err(&pdev->dev, "ioremap failed");
2886 goto err_map_bam;
2887 }
2888
Liron Kuch59339922013-01-01 18:29:47 +02002889 if (msm_tspp_map_irqs(pdev, device))
Joel Nider5556a852011-10-16 10:52:13 +02002890 goto err_irq;
Joel Nider5556a852011-10-16 10:52:13 +02002891
Joel Nider5556a852011-10-16 10:52:13 +02002892 /* power management */
2893 pm_runtime_set_active(&pdev->dev);
2894 pm_runtime_enable(&pdev->dev);
2895
Joel Nider5556a852011-10-16 10:52:13 +02002896 tspp_debugfs_init(device, 0);
2897
2898 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2899 tsif_debugfs_init(&device->tsif[i], i);
Joel Nider5556a852011-10-16 10:52:13 +02002900
2901 wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
2902 dev_name(&pdev->dev));
2903
2904 /* set up pointers to ram-based 'registers' */
Joel Nider435ad8e2011-12-14 16:53:30 +02002905 device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
2906 device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
2907 device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
2908 device->tspp_key_table = device->base + TSPP_DATA_KEY;
2909 device->tspp_global_performance =
2910 device->base + TSPP_GLOBAL_PERFORMANCE;
2911 device->tspp_pipe_context =
2912 device->base + TSPP_PIPE_CONTEXT;
2913 device->tspp_pipe_performance =
2914 device->base + TSPP_PIPE_PERFORMANCE;
Joel Nider5556a852011-10-16 10:52:13 +02002915
2916 device->bam_props.summing_threshold = 0x10;
2917 device->bam_props.irq = device->bam_irq;
2918 device->bam_props.manage = SPS_BAM_MGR_LOCAL;
2919
Liron Kuch59339922013-01-01 18:29:47 +02002920 if (tspp_clock_start(device) != 0) {
2921 dev_err(&pdev->dev, "Can't start clocks");
2922 goto err_clock;
2923 }
2924
Joel Nider5556a852011-10-16 10:52:13 +02002925 if (sps_register_bam_device(&device->bam_props,
2926 &device->bam_handle) != 0) {
2927 pr_err("tspp: failed to register bam");
2928 goto err_bam;
2929 }
2930
Joel Nider5556a852011-10-16 10:52:13 +02002931 spin_lock_init(&device->spinlock);
2932 tasklet_init(&device->tlet, tspp_sps_complete_tlet,
2933 (unsigned long)device);
2934
2935 /* initialize everything to a known state */
2936 tspp_global_reset(device);
2937
2938 version = readl_relaxed(device->base + TSPP_VERSION);
Liron Kuch59339922013-01-01 18:29:47 +02002939 /*
2940 * TSPP version can be bits [7:0] or alternatively,
2941 * TSPP major version is bits [31:28].
2942 */
2943 if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1))
Joel Nider5556a852011-10-16 10:52:13 +02002944 pr_warn("tspp: unrecognized hw version=%i", version);
2945
Joel Nider435ad8e2011-12-14 16:53:30 +02002946 /* initialize the channels */
Joel Nider5556a852011-10-16 10:52:13 +02002947 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002948 if (tspp_channel_init(&(device->channels[i]), device) != 0) {
2949 pr_err("tspp_channel_init failed");
2950 goto err_channel;
2951 }
Joel Nider5556a852011-10-16 10:52:13 +02002952 }
2953
Joel Nider435ad8e2011-12-14 16:53:30 +02002954 /* stop the clocks for power savings */
2955 tspp_clock_stop(device);
2956
2957 /* everything is ok, so add the device to the list */
2958 list_add_tail(&(device->devlist), &tspp_devices);
2959
Joel Nider5556a852011-10-16 10:52:13 +02002960 return 0;
2961
Joel Nider435ad8e2011-12-14 16:53:30 +02002962err_channel:
Liron Kuch59339922013-01-01 18:29:47 +02002963 /* un-initialize channels */
Liron Kuch72b78552012-10-30 17:47:50 +02002964 for (j = 0; j < i; j++) {
2965 channel = &(device->channels[i]);
2966 device_destroy(tspp_class, channel->cdev.dev);
2967 cdev_del(&channel->cdev);
2968 }
Liron Kuch59339922013-01-01 18:29:47 +02002969
Joel Nider5556a852011-10-16 10:52:13 +02002970 sps_deregister_bam_device(device->bam_handle);
Liron Kuch59339922013-01-01 18:29:47 +02002971err_clock:
Joel Nider5556a852011-10-16 10:52:13 +02002972err_bam:
Joel Nider5556a852011-10-16 10:52:13 +02002973 tspp_debugfs_exit(device);
2974 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2975 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02002976err_irq:
Liron Kuch59339922013-01-01 18:29:47 +02002977 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2978 if (device->tsif[i].tsif_irq)
2979 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
2980 }
2981 if (device->tspp_irq)
2982 free_irq(device->tspp_irq, device);
2983
Joel Nider5556a852011-10-16 10:52:13 +02002984 iounmap(device->bam_props.virt_addr);
2985err_map_bam:
2986err_res_bam:
2987 iounmap(device->base);
2988err_map_dev:
2989err_res_dev:
2990 iounmap(device->tsif[1].base);
2991err_map_tsif1:
2992err_res_tsif1:
2993 iounmap(device->tsif[0].base);
2994err_map_tsif0:
2995err_res_tsif0:
2996 if (device->tsif_ref_clk)
2997 clk_put(device->tsif_ref_clk);
2998err_refclock:
2999 if (device->tsif_pclk)
3000 clk_put(device->tsif_pclk);
3001err_pclock:
3002 kfree(device);
3003
3004out:
3005 return rc;
3006}
3007
3008static int __devexit msm_tspp_remove(struct platform_device *pdev)
3009{
Joel Nider435ad8e2011-12-14 16:53:30 +02003010 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02003011 u32 i;
Joel Nider5556a852011-10-16 10:52:13 +02003012
3013 struct tspp_device *device = platform_get_drvdata(pdev);
3014
Joel Nider435ad8e2011-12-14 16:53:30 +02003015 /* free the buffers, and delete the channels */
3016 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
3017 channel = &device->channels[i];
3018 tspp_close_channel(device->pdev->id, i);
3019 device_destroy(tspp_class, channel->cdev.dev);
3020 cdev_del(&channel->cdev);
3021 }
3022
Liron Kuch59339922013-01-01 18:29:47 +02003023 /* de-registering BAM device requires clocks */
3024 tspp_clock_start(device);
Joel Nider5556a852011-10-16 10:52:13 +02003025 sps_deregister_bam_device(device->bam_handle);
Liron Kuch59339922013-01-01 18:29:47 +02003026 tspp_clock_stop(device);
Joel Nider5556a852011-10-16 10:52:13 +02003027
Hamad Kadmany44307d32012-11-25 09:49:51 +02003028 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
Joel Nider5556a852011-10-16 10:52:13 +02003029 tsif_debugfs_exit(&device->tsif[i]);
Hamad Kadmany44307d32012-11-25 09:49:51 +02003030 if (device->tsif[i].tsif_irq)
3031 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
3032 }
Joel Nider5556a852011-10-16 10:52:13 +02003033
3034 wake_lock_destroy(&device->wake_lock);
3035 free_irq(device->tspp_irq, device);
Joel Nider5556a852011-10-16 10:52:13 +02003036
3037 iounmap(device->bam_props.virt_addr);
3038 iounmap(device->base);
3039 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3040 iounmap(device->tsif[i].base);
3041
3042 if (device->tsif_ref_clk)
3043 clk_put(device->tsif_ref_clk);
3044
3045 if (device->tsif_pclk)
3046 clk_put(device->tsif_pclk);
3047
3048 pm_runtime_disable(&pdev->dev);
3049 pm_runtime_put(&pdev->dev);
3050 kfree(device);
3051
3052 return 0;
3053}
3054
3055/*** power management ***/
3056
3057static int tspp_runtime_suspend(struct device *dev)
3058{
3059 dev_dbg(dev, "pm_runtime: suspending...");
3060 return 0;
3061}
3062
3063static int tspp_runtime_resume(struct device *dev)
3064{
3065 dev_dbg(dev, "pm_runtime: resuming...");
3066 return 0;
3067}
3068
3069static const struct dev_pm_ops tspp_dev_pm_ops = {
3070 .runtime_suspend = tspp_runtime_suspend,
3071 .runtime_resume = tspp_runtime_resume,
3072};
3073
Liron Kuch59339922013-01-01 18:29:47 +02003074static struct of_device_id msm_match_table[] = {
3075 {.compatible = "qcom,msm_tspp"},
3076 {}
3077};
3078
Joel Nider5556a852011-10-16 10:52:13 +02003079static struct platform_driver msm_tspp_driver = {
3080 .probe = msm_tspp_probe,
3081 .remove = __exit_p(msm_tspp_remove),
3082 .driver = {
3083 .name = "msm_tspp",
3084 .pm = &tspp_dev_pm_ops,
Liron Kuch59339922013-01-01 18:29:47 +02003085 .of_match_table = msm_match_table,
Joel Nider5556a852011-10-16 10:52:13 +02003086 },
3087};
3088
3089
3090static int __init mod_init(void)
3091{
Joel Nider5556a852011-10-16 10:52:13 +02003092 int rc;
3093
Joel Nider435ad8e2011-12-14 16:53:30 +02003094 /* make the char devs (channels) */
Joel Nider5556a852011-10-16 10:52:13 +02003095 rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
3096 if (rc) {
3097 pr_err("tspp: alloc_chrdev_region failed: %d", rc);
3098 goto err_devrgn;
3099 }
3100
3101 tspp_class = class_create(THIS_MODULE, "tspp");
3102 if (IS_ERR(tspp_class)) {
3103 rc = PTR_ERR(tspp_class);
3104 pr_err("tspp: Error creating class: %d", rc);
3105 goto err_class;
3106 }
3107
Joel Nider435ad8e2011-12-14 16:53:30 +02003108 /* register the driver, and check hardware */
3109 rc = platform_driver_register(&msm_tspp_driver);
3110 if (rc) {
3111 pr_err("tspp: platform_driver_register failed: %d", rc);
3112 goto err_register;
Joel Nider5556a852011-10-16 10:52:13 +02003113 }
3114
3115 return 0;
3116
Joel Nider435ad8e2011-12-14 16:53:30 +02003117err_register:
3118 class_destroy(tspp_class);
Joel Nider5556a852011-10-16 10:52:13 +02003119err_class:
3120 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
3121err_devrgn:
Joel Nider5556a852011-10-16 10:52:13 +02003122 return rc;
3123}
3124
3125static void __exit mod_exit(void)
3126{
Joel Nider435ad8e2011-12-14 16:53:30 +02003127 /* delete low level driver */
3128 platform_driver_unregister(&msm_tspp_driver);
Joel Nider5556a852011-10-16 10:52:13 +02003129
Joel Nider435ad8e2011-12-14 16:53:30 +02003130 /* delete upper layer interface */
Joel Nider5556a852011-10-16 10:52:13 +02003131 class_destroy(tspp_class);
3132 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
Joel Nider5556a852011-10-16 10:52:13 +02003133}
3134
3135module_init(mod_init);
3136module_exit(mod_exit);
3137
Joel Nider435ad8e2011-12-14 16:53:30 +02003138MODULE_DESCRIPTION("TSPP platform device and char dev");
Joel Nider5556a852011-10-16 10:52:13 +02003139MODULE_LICENSE("GPL v2");